字符流出现的原因及编码表概述和常见编码表
字符流出现的原因:由于字节流操作中文不是特别方便,所以,java就提供了字符流
常见的字符编码表:
ASCII(数字、英文):1个字符占一个字节(所有的编码集都兼容ASCII)
• ISO8859-1(欧洲):1个字符占一个字节
• GB-2312/GBK:1个字符占两个字节
• Unicode: 1个字符占两个字节(网络传输速度慢)
• UTF-8:变长字节,对于英文一个字节,对于汉字两个或三个字节
字符流 = 字节流 + 编码表
String类中的编码和解码问题
编码: 就是把字符串转换成字节数组
- 把一个字符串转换成一个字节数组
- public byte[] getBytes();使用平台的默认字符集将此 String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
public byte[] getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
- public byte[] getBytes();使用平台的默认字符集将此 String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
-
- 解码: 把字节数组转换成字符串
public String(byte[] bytes): 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
public String(byte[] bytes, String charsetName) 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
- 解码: 把字节数组转换成字符串
String类中的编码和解码问题
默认的字符集是GBK的
String(byte[] bytes, String charsetName):通过指定的字符集解码字节数组
byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组
编码:把看得懂的变成看不懂的: String – byte[]
解码:把看不懂的变成看得懂的: byte[] – String
转换流OutputStreamWriter的使用
OutputStreamWriter的构造方法
OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流
OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流
字符流的5种写数据的方式
public void write(int c) 写一个字符
public void write(char[] cbuf) 写一个字符数组
public void write(char[] cbuf,int off,int len) 写一个字符数组的 一部分
public void write(String str) 写一个字符串
public void write(String str,int off,int len) 写一个字符串的一部分
转换流InputStreamReader的使用
InputStreamReader的构造方法
InputStreamReader(InputStream is):用默认的编码(GBK)读取数据
InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
字符流的2种读数据的方式
public int read() 一次读取一个字符
public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1
字符流复制文本文件
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader (new FileInputStream ("s.txt"));
OutputStreamWriter out = new OutputStreamWriter (new FileOutputStream ("s2.txt"));
char[] chars = new char[1000];
int len=0;
while ((len=in.read (chars))!=-1){
out.write (chars,0,len);
out.flush ();
}
in.close ();
out.close ();
}
FileWriter和FileReader复制文本文件
FileReader和FileWriter的出现
转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,
所以,为了简化我们的书写,转换流提供了对应的子类。
FileWriter
FileReader
字符流便捷类: 因为转换流的名字太长了,并且在一般情况下我们不需要制定字符集,
于是java就给我们提供转换流对应的便捷类
转换流 便捷类
OutputStreamWriter ------- FileWriter
InputStreamReader ------- FileReader
字符缓冲流的基本使用
高效的字符流
高效的字符输出流: BufferedWriter
构造方法: public BufferedWriter(Writer w)
高效的字符输入流: BufferedReader
构造方法: public BufferedReader(Reader e)
字符缓冲流的特殊功能
BufferedWriter: public void newLine():根据系统来决定换行符 具有系统兼容性的换行符
BufferedReader: public String readLine():一次读取一行数据 是以换行符为标记的 读到换行符就换行 没读到数据返回null
包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
字符缓冲流的特殊功能复制文本文件
public static void main(String[] args) throws IOException {
BufferedWriter out = new BufferedWriter (new FileWriter ("e.txt"));
BufferedReader in = new BufferedReader (new FileReader ("fos.txt"));
String s =null;
while ((s=in.readLine ())!=null){
out.write (s);
out.newLine ();
out.flush ();
}
in.close ();
out.close ();
}
复制多级文件夹
使用递归,先判断是否为文件夹,是的话创建,不是的话,复制文件
public static void main(String[] args) throws IOException {
File file = new File("D:\\音乐");
File file1 = new File("G:\\无损音乐");
copy (file,file1);
}
private static void copy(File filea, File fileb) throws IOException {
File[] files = fileb.listFiles();
for (File file2 : files) {
if (file2.isFile ()) {
FileInputStream in = new FileInputStream (new File (file2.getAbsolutePath ()));
FileOutputStream out = new FileOutputStream (new File (filea,file2.getName ()));
byte[] bytes = new byte[1024 * 8];
int len = 0;
while ((len = in.read (bytes)) != -1) {
out.write (bytes, 0, len);
}
}else {
File file3 = new File (filea,file2.getName ());
file3.mkdir ();
copy (file3,file2);
}
}
}
复制指定目录下指定后缀名的文件并修改名称
public static void main(String[] args) throws IOException {
File file = new File ("D:\\测试");
File file1 = new File ("D:\\音乐");
File[] files = file1.listFiles ();
for (File file2 : files) {
FileInputStream in = new FileInputStream (new File (file2.getAbsolutePath ()));
FileOutputStream out = new FileOutputStream (new File (file, file2.getName ()));
byte[] bytes = new byte[1024 * 8];
int len = 0;
while ((len = in.read (bytes)) != -1) {
out.write (bytes, 0, len);
out.flush ();
}
out.close ();
}
File[] files1 = file.listFiles ();
for (File file2 : files1) {
if (file2.getName ().endsWith (".png")) {
File file3 = new File (file2.getAbsolutePath ().replace (".png", ".jpg"));
file2.renameTo (file3);
}
}
}
键盘录入学生信息按照总分排序并写入文本文件
- 分析:
- a: 创建一个学生类: 姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)
- b: 因为要排序,所以需要选择TreeSet进行存储学生对象
- c: 键盘录入学生信息,把学生信息封装成一个学生对象,在把学生对象添加到集合中
- d: 创建一个高效的字符输出流对象
- e: 遍历集合,把学生的信息写入到指定的文本文件中
- f: 释放资源
public class Test3 {
public static void main(String[] args) throws IOException {
TreeSet<Student> students = new TreeSet<> (new Comparator<Student> () {
@Override
public int compare(Student o1, Student o2) {
int num=o1.TotalScore ()-o2.TotalScore ();
int num2=num==0?o1.getName ().compareTo (o2.getName ()):num;
return -num2;
}
});
for (int i = 1; i <=5; i++) {
Scanner scanner = new Scanner (System.in);
System.out.println ("请输入第"+i+"个学生的姓名");
String name = scanner.nextLine ();
System.out.println ("请输入第"+i+"个学生的语文成绩");
int i1 = scanner.nextInt ();
System.out.println ("请输入第"+i+"个学生的数学成绩");
int i2 = scanner.nextInt ();
System.out.println ("请输入第"+i+"个学生的英语成绩");
int i3 = scanner.nextInt ();
Student student = new Student (name, i1, i2, i3);
students.add (student);
}
BufferedWriter score = new BufferedWriter (new FileWriter ("StudentScore", true));
score.write ("名次"+" "+"姓名"+" "+"语文成绩"+" "+"数学成绩"+" "+"英语成绩"
+" "+"总成绩");
score.newLine ();
score.flush ();
int index=1;
for (Student student : students) {
score.write (index+" "+student.getName ()+" "+student.getChineseScore ()+" "
+student.getMathScore ()+" "+student.getEnglishScore ()+" "+student.TotalScore ());
score.newLine ();
score.flush ();
index++;
}
score.close ();
System.out.println ("Finished");
}
}
class Student{
private String name;
private int ChineseScore;
private int MathScore;
private int EnglishScore;
public Student(String name, int chineseScore, int mathScore, int englishScore) {
this.name = name;
ChineseScore = chineseScore;
MathScore = mathScore;
EnglishScore = englishScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChineseScore() {
return ChineseScore;
}
public void setChineseScore(int chineseScore) {
ChineseScore = chineseScore;
}
public int getMathScore() {
return MathScore;
}
public void setMathScore(int mathScore) {
MathScore = mathScore;
}
public int getEnglishScore() {
return EnglishScore;
}
public void setEnglishScore(int englishScore) {
EnglishScore = englishScore;
}
public int TotalScore(){
return this.ChineseScore+this.MathScore+this.EnglishScore;
}
}