今天学习的内容是流的操作规律和编解码
一、流的操作规律
JavaIO流的类有很多,想要知道开发时到底用哪个,通过四个明确即可:
- 明确源和目的地。源使用InputStream或Reader;目的地使用OutputStream和Writer
- 明确数据是否为纯文本。纯文本数据使用Reader或Writer;非纯文本数据使用InputStream或OutputStreamWriter
- 明确具体设备。源设备有硬盘、键盘、内存和网络;目的地设备有硬盘、控制台、内存和网络
- 明确是否需要额外功能。是否需要缓冲区等等
二、编解码
之前说FileWriter和FileReader与流的转换是等价的,其实这个说法不太严谨,因为FileWriter和FileReader只能使用默认编码表
,而OutputStreamWriter和InputStreamReader可以指定编码表(使用流的转换的好处)。下面结合程序示例说明编码表的使用:
public class Test82 {
public static void main(String[] args) {
writeText_1();
writeText_2();
readText_1();
readText_2();
}
private static void readText_2() {
InputStreamReader isr_1 = null;
InputStreamReader isr_2 = null;
try {
isr_1 = new InputStreamReader(new FileInputStream("gbk.txt"),"utf-8");
int len_1 = 0;
char[] buf_1 = new char[1024];
while ((len_1 = isr_1.read(buf_1)) != -1) {
System.out.println(new String(buf_1, 0, len_1));// 你好(其实结果应该是乱码,但是本eclipse设置编码表为utf_8)
}
isr_2 = new InputStreamReader(new FileInputStream("u8.txt"),"utf-8");
int len_2 = 0;
char[] buf_2 = new char[1024];
while ((len_2 = isr_2.read(buf_2)) != -1) {
System.out.println(new String(buf_2, 0, len_2));// 你好
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isr_1 != null) {
try {
isr_1.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
if (isr_2 != null) {
try {
isr_2.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
private static void readText_1() {
FileReader fr_1 = null;
FileReader fr_2 = null;
try {
fr_1 = new FileReader("gbk.txt");
int len_1 = 0;
char[] buf_1 = new char[1024];
while ((len_1 = fr_1.read(buf_1)) != -1) {
System.out.println(new String(buf_1, 0, len_1));// 你好
}
fr_2 = new FileReader("u8.txt");
int len_2 = 0;
char[] buf_2 = new char[1024];
while ((len_2 = fr_2.read(buf_2)) != -1) {
System.out.println(new String(buf_2, 0, len_2));// 你好(其实结果应该是乱码,但是本eclipse设置编码表为utf_8)
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr_1 != null) {
try {
fr_1.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
if (fr_2 != null) {
try {
fr_2.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
private static void writeText_2() {
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream("u8.txt"), "utf-8");
osw.write("你好");
osw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
private static void writeText_1() {
FileWriter fw = null;
try {
fw = new FileWriter("gbk.txt");
fw.write("你好");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
}
在进行读写操作时,一定要注意编码表的统一!由于本eclipse设置的编码表为UTF-8,所以实际上所有文件的编码都使用了UTF-8,故写入的两个文件都是6字节的。UTF-8最多用三个字节表示一个中文字符,而GBK用两个字节表示一个中文字符,所以实际上用UTF-8解码使用GBK编码的文件时,会出现“??”乱码;用GBK解码使用Unicode编码的文件时,会出现未知乱码。下面这个例子很明白地说明了这一点:
public class EncodeAndDecode {
public static void main(String[] args) throws IOException {
String s = "你好";
//编码
byte[] buf_utf = s.getBytes();// 默认使用UTF-8对字符串进行编码
for (byte b : buf_utf) {
System.out.print(b+" ");// -28 -67 -96 -27 -91 -67 UTF-8使用三个字节代表一个中文字符
}
byte[] buf_gbk = s.getBytes("gbk");// 使用GBK对字符串进行编码
for (byte b : buf_gbk) {
System.out.print(b+" ");// 60 -29 -70 -61 GBK使用两个字节代表一个中文字符
}
//解码
String str_1 = new String(buf_utf);//使用默认的UTF-8解码使用UTF-8编码的字节
System.out.println(str_1);// 你好
String str_2 = new String(buf_gbk);//使用默认的UTF-8解码使用GBK编码的字节
System.out.println(str_2);// ���
String str_3 = new String(buf_gbk,"gbk");//使用GBK解码使用GBK编码的字节
System.out.println(str_3);// 你好
String str_4 = new String(buf_utf,"gbk");//使用GBK解码使用UTF-8编码的字节
System.out.println(str_4);// 浣犲ソ
}
}