一、编码问题
见之前的博客:https://blog.csdn.net/womeng2009/article/details/84187262
二、文本和文本文件
Java中文本(char)是16位无符号整数,是字符的unicode编码(双字节编码)
文件是byte byte byte…的数据序列
文本文件指的文本(char)序列按照某种编码方案(utf-8、utf-16be、gbk)序列化为byte的存储结果
三、字符流(Reader/Writer)- 输入输出流的抽象类
字符的处理,一次处理一个字符。字符的底层仍然是基本的字节序列。
四、字符流的基本实现
InputStreamReader 完成byte流解析为char流,按照编码解析
OutputStreamWriter 提供char流到byte流,按照编码处理
程序示例:
package com.io;
import java.io.*;
public class IsrAndOsw {
public static void main(String[] args) {
Reader reader = null;
Writer writer = null;
try {
reader = new InputStreamReader(
new FileInputStream("/Users/xxx/Downloads/io_test/random_file1.txt"), "gbk");
writer = new OutputStreamWriter(
new FileOutputStream("/Users/xxx/Downloads/io_test/random_file2.txt"), "gbk");
char[] readBuff = new char[1024];
int b;
while ((b = reader.read(readBuff, 0, readBuff.length)) != -1) {
writer.write(readBuff, 0, b);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileOutputStream输出结果:
五、字符流之文件读写流
FileReader/FileWriter
缺点:不能指定编码。
程序示例:
package com.io;
import java.io.*;
public class FileRW {
public static void main(String[] args) {
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
//不能指定编码,如果跟项目编码不同,只能回归到InputStreamReader/OutputStreamWriter
fileReader = new FileReader(new File("/Users/xxx/Downloads/io_test/random_file1.txt"));
fileWriter = new FileWriter(new File("/Users/xxx/Downloads/io_test/random_file3.txt"));
char[] buff = new char[1024];
int b;
while ((b = fileReader.read(buff, 0, buff.length)) != -1) {
fileWriter.write(buff, 0, b);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileWriter输出结果:
六、字符流的过滤器
BufferedReader 可以readLine,一次读一行
BufferedWriter/PrintWriter 可以写一行(可结合PrintWriter操作)
package com.io;
import java.io.*;
public class BwAndBr {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
PrintWriter print = null;
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream("/Users/xxx/Downloads/io_test/random_file1.txt"), "gbk"));
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("/Users/xxx/Downloads/io_test/random_file4.txt"), "gbk"));
print = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("/Users/xxx/Downloads/io_test/random_file5.txt"), "gbk")));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();//必须刷新缓冲区
//PrintWriter方式
print.println(line);//带换行
print.print(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (print != null) {
print.close();
}
}
}
}
BufferedWriter输出结果:
PrintWriter输出结果: