乱码问题
从文件中读取出现乱码主要有以下两个原因:
- 编码字符集不统一
- 截取长度不不够
解决方法
将文件以字节流读入,转换为字符流操作
代码语言:javascript复制字节流读入 --InputStreamReader–> 字符缓冲流读入 字节流写出 --OutputStreamReader–> 字符缓冲流写出
new InputStreamReader();
new OutputStreamWriter();
两个问题示例代码
代码语言:javascript复制 1 package cn.hxh.io.convert;
2
3 import java.io.UnsupportedEncodingException;
4
5 public class convertDemo01 {
6
7 public static void main(String[] args) throws UnsupportedEncodingException {
8 String str = "你好";
9 byte[] data = str.getBytes("gbk");//指定编码
10 byte[] data1 = str.getBytes();
11 System.out.println(new String(data,"utf-8"));
12 System.out.println(new String(data1,1,3));//长度不够
13 }
14
15 }
引入转换流
代码语言:javascript复制 1 package cn.hxh.io.convert;
2
3 import java.io.*;
4
5 public class convertDemo02 {
6
7 public static void main(String[] args) throws IOException {
8 BufferedReader rd = new BufferedReader(
9 new InputStreamReader(
10 new FileInputStream(
11 new File("D:/aa/a.txt")), "utf-8"));
12 BufferedWriter wr = new BufferedWriter(
13 new OutputStreamWriter(
14 new FileOutputStream(
15 new File("D:/aa/b.txt"))));
16 String s = null;
17 while(null != (s = rd.readLine())) {
18 wr.write(s);
19 wr.newLine();
20 wr.flush();
21 }
22 wr.close();
23 rd.close();
24 }
25
26 }