Java字符流
字符流
字符编码
编码和解码方式不同,会出现乱码
- ISO-8859-1:收录除ASCII外,还包括西欧、希腊语、泰语、阿拉伯语、希伯来语对应的文字符号。
- UTF-8:针对Unicode码表的可变长度字符编码,用1/2/3个字节表示字符(万国码)
- GB2312:简体中文,1/2个字节
- GBK:简体中文、扩充
- BIG5:台湾,繁体中文
字节流在读取中文时会将中文拆成单独字节读取,会读成乱码
字符流
- 字符流的父类(抽象类)
- Reader:字符输入流
- read()
- Writer:字符输出流
- write()
- Reader:字符输入流
文件字符流
- FileReader:
- read(char[] c):从流中读取多个字符,将读到的内容存入数组,返回实际读到的字符数;文件尾部返回-1
- FileWriter:
- write(String str):一次写入多个字符,将b数组中所有字符写入输出流
FileReader
单字读取:
//1创建FileReader 文件字符输入流
FileReader fileReader = new FileReader("F:\\a.txt");
//2读取
int data = 0;
while((data = fileReader.read()) != -1){
System.out.print((char)data);
}
fileReader.close();
字符串读取:
char[] buf = new char[1024];
int count = 0;
while((count = fileReader.read(buf))!= -1){
System.out.println(new String(buf,0,count));
}
FileWriter
//1 创建FileWriter对象
FileWriter fileWriter = new FileWriter("F:\\write.txt");
//2 写入
for (int i = 0; i < 10; i++) {
fileWriter.write("java是最好的语言\r\n");
fileWriter.flush();
}
fileWriter.close();
复制文本文件(不能复制二进制文件)
字符流读的是字符,图片和其他二进制文件无法读取,会变成乱码。
FileReader fileReader = new FileReader("F:\\write.txt");
FileWriter fileWriter = new FileWriter("F:\\write2.txt");
//write
int data = 0;
while((data = fileReader.read())!=-1){
fileWriter.write(data);
fileWriter.flush();
}
fileReader.close();
fileWriter.close();
字符缓冲流
- 缓冲流:
- BufferedReader
- BufferedWriter
- 高效读写
- 支持输入换行符
- 可一次写一行,读一行
BufferedReader
-
read()
-
readLine():读取一行
-
普通读取
FileReader fileReader = new FileReader("F:\\write.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
//read
char[] buf = new char[1024];
int count = 0;
while((count = bufferedReader.read(buf))!= -1){
System.out.println(new String(buf,0,count));
}
bufferedReader.close();
- 按行读取
FileReader fileReader = new FileReader("F:\\write.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
//read
String line = null;
while ((line = bufferedReader.readLine())!=null){
System.out.println(line);
}
bufferedReader.close();
BufferedWriter
- write()
- newLine():写入一个行分隔符
FileWriter fileWriter = new FileWriter("F:\\buffer.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//write
for (int i = 0; i < 9; i++) {
bufferedWriter.write("cheer on");
bufferedWriter.newLine();//写入一个换行符
bufferedWriter.flush();
}
bufferedWriter.close();
打印流
- PrintWriter:
- 封装了print()/println()方法,支持写入后换行
- 支持数据原样打印
//创建打印流
PrintWriter printWriter = new PrintWriter("F:\\print.txt");
//打印
printWriter.println(97);//打印结果就是数字97
printWriter.println(true);
printWriter.println('c');
printWriter.println(3.62);
printWriter.println("abcdd");
//关闭
printWriter.close();
System.out.println("打印完毕");
转换流
- 本质是 内存的中的字符 和 硬盘中的字节 的转换
- 也叫桥转换流:
- InputStreamReader
- OutputStreamReader
- 可将字节流转换为字符流
- 可设置字符的编码方式
读取文件
可在创建 InputStreamReader
对象时设置编码格式
//1.创建对象,指定读取文件的编码方式
FileInputStream fileInputStream = new FileInputStream("F:\\write.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");
//2.读文件
int data = 0;
while ((data = inputStreamReader.read())!=-1){
System.out.print((char)data);
}
//3.close
inputStreamReader.close();
写入文件
可在创建 OutputStreamReader
对象时设置编码格式
FileOutputStream fileOutputStream = new FileOutputStream("F:\\output.txt");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"gbk");
for (int i = 0; i < 9; i++) {
outputStreamWriter.write("天气真好\r\n");
outputStreamWriter.flush();
}
outputStreamWriter.close();