1. 字符编码
- ISO-8859-1 收录除 ASCII 外,还包括西欧、希腊语、泰语、阿拉伯语、希伯来语对应的文字符号
- UTF-8 针对 Unicode 码表的可变长度字符编码
- GB2312 简体中文
- GBK 简体中文扩充
- BIG5 台湾,繁体中文
当编码方式和解码方式不一致时,会出现乱码
1.1 各种编码占用字节数
编码 | 英文(Byte) | 中文(Byte) |
---|---|---|
ISO-8859-1 | 1 | 1 |
UTF-8 | 1 | 3 |
UTF-16 | 4 | 4 |
GB2312 | 1 | 2 |
GBK | 1 | 2 |
BIG5 | 1 | 2 |
2. 字符流
java.io.FileInputStream#read() 是一个字节一个字节读取的,直接读取中文时会按单个字节读取,改用字符流
2.1 抽象父类 Reader / Writer
Reader 字符输入流
Writer 字符输出流
public abstract class Reader implements Readable, Closeable {
// Reads a single character
public int read() throws IOException {}
}
2.2 文件字符流 FileReader/FileWriter
Reader reader = new FileReader("src/main/resources/demo.txt");
int length = 0;
// 读取一个字符
while ((length = reader.read()) != -1) {
System.out.println((char) length);
}
增加缓冲区
Reader reader = new FileReader("src/main/resources/demo.txt");
// 字符数组
char[] buf = new char[1024];
int length = 0;
while ((length = reader.read(buf)) != -1) {
System.out.println(new String(buf, 0, length));
}
reader.close();
Writer writer = new FileWriter("src/main/resources/out.txt");
for (int i = 0; i < 10; i++) {
writer.write("中文测试\r\n");
writer.flush();
}
writer.close();
2.3 字符流复制文件
FileReader 和 FileWriter 复制文本文件,不能复制图片或二进制文件(要用字节流)
原因是字符流在读取时是有编码的,图片是没有字符编码的,读取的二进制流就不能转化成字符信息,变为乱码
所有的文件存在硬盘上都是二进制流,但文本文件有字符编码,图片没有字符编码,字节流可以读取任何文件
public static void main(String[] args) throws IOException {
Reader reader = new FileReader("src/main/resources/demo.txt");
Writer writer = new FileWriter("src/main/resources/out.txt");
int length = 0;
while ((length = reader.read()) != -1) {
writer.write(length);
writer.flush();
}
reader.close();
writer.close();
}
3. 字符缓冲流
可读写一行,支持输入换行符
public class BufferedReader extends Reader {
public String readLine() throws IOException {
return readLine(false);
}
}
public class BufferedWriter extends Writer {
// Writes a line separator
public void newLine() throws IOException {
write(lineSeparator);
}
}
3.1 BufferedReader
BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/demo.txt"));
char[] buf = new char[3];
int length = 0;
while ((length = reader.read(buf)) != -1) {
System.out.println(new String(buf, 0, length));
}
reader.close();
一行一行的读取
BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/demo.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
3.2 BufferedWriter
BufferedWriter writer = new BufferedWriter(new FileWriter("src/main/resources/out.txt"));
for (int i = 0; i < 10; i++) {
writer.write("写入测试");
writer.newLine();
writer.flush();
}
writer.close();
4. 打印流(PrintWriter)
- 封装 print() / println() , 支持写入后换行
- 支持数据原样打印
PrintWriter printWriter = new PrintWriter("src/main/resources/print.txt");
printWriter.println(97);
printWriter.println(true);
printWriter.print(3.14);
printWriter.print('a');
printWriter.close();
5. 转换流
- 桥转换流:InputStreamReader / OutputStreamWriter
- 可将字节流转换为字符流
- 可设置字符的编码方式
- 内存中的字符与硬盘中的字节之间的转换
InputStreamReader isr = new InputStreamReader(new FileInputStream("src/main/resources/demo.txt"), "UTF-8");
int data = 0;
while ((data = isr.read()) != -1) {
System.out.println((char) data);
}
isr.close();
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("src/main/resources/out.txt"), StandardCharsets.UTF_8);
for (int i = 0; i < 10; i++) {
osw.write("这是一个测试");
}
osw.close();
5.1 InputStreamReader / OutputStreamWriter
网友评论