今天在学习Java IO流的时候遇到了一个问题,记录一下
- 我创建了一个文本文件,内容为“我” ,使用utf-8存储
- 用字节流读取文件
public static void main(String[] args) {
InputStream inputStream=null;
try {
inputStream = new FileInputStream(new File("d:/test2.txt"));
int result=0;
while((result=inputStream.read())!=-1){
System.out.println(result);
}
} catch (FileNotFoundException fileNotFoundException){
fileNotFoundException.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}finally{
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException ioException) {
ioException.getMessage();
}
}
}
- 调试查看result的值,发现他的值分别为 0xE6 , 0x88 和 0x91, utf-8编码一个汉字占3个字节没错,结果没什么问题,但是当时我就有了个疑惑,一个汉字3个字节,而字符流中一个char才2个字节,它怎么读的?
- 用字符流读取文件
public static void main(String[] args) {
Reader reader=null;
try {
reader = new FileReader(new File("d:/test2.txt"));
int result=0;
while((result=reader.read())!=-1){
System.out.println(result);
}
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (reader!= null) {
reader.close();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
- 查看控制台输出了”我“,成功了,怎么回事,说好的1个char2个字节呢,这个汉字占了3个字节,怎么不按套路???
- 调试查看result的值,发现result的值为0x6211,而0x6211正好是汉字“我”的Unicode的值
- 因为字符流是使用字符数组读取的,而一个字符只能容纳2个字符,所以它存储的是字符的Unicode值,而不是字符在磁盘中存储的值。
- 也就是说如果一个文件使用非Unicode编码存储时,字符流读取会乱码,要想读取非Unicode文本文件,可以使用以下代码,其中gbk可以切换成你想要的编码方式
reader = new InputStreamReader(new FileInputStream(new File("d:/test2.txt")),"gbk");
本人Java小白,以上内容可能有错误,如发现希望指明,共同进步。