package com.yaxing.io;import java.io.FileReader;
import java.io.IOException;public class FileReaderDemo { /**
* @param args
*/
public static void main(String[] args) {
FileReader fw = null;
try {
fw = new FileReader("c:\\w.txt");
// int ch = 0;
// while ((ch = fw.read()) != -1) {
// System.out.println("读取:" + (char) ch);
// }
while ((fw.read()) != -1) {
System.out.println("读取:" + (char)fw.read());
}
} catch (IOException e) { e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) { e.printStackTrace();
}
}
}
}}
上面注释的部分读取,输出正常
而使用了下面的输出,则不正常,只输出了部分数据,为啥呢?
读取:s
读取:c
读取:1
读取:3
读取:文本文件内容如下:
asdcr1231
while ((fw.read()) != -1) {
System.out.println("读取:" + (char)fw.read());
}这里面两个 fw.read()的值不一样吗?
另外 直接输出 fw.read()这个输出的是啥呢?ascii码还是?
System.out.println("读取:" + (char)fw.read());
}
System.out.println("读取:" + (char)fw.read());//这里又读了一次
}你输出的都是第二次读的,当第二次读的是最后一个时,也没有检查到,再来读第一次时,就会出错。