FileInputStream读取时为什么隔一个一读取。

File file = new File("E:\\hello.txt");
FileInputStream fos = new FileInputStream(file);
try {
while (fos.read()!=-1) {
System.out.print((char)fos.read()+"");
}
为什么读东西的时候第一个不读读第二个,比如文件是123456789
读出的是2468?。

因为你的fos.read()这个读取方法在while里面执行了一次之后,在循环体里面又执行了一次啊。你要一个一个读就应该这样写:

int n = -1;
while ((n = fos.read()) != -1) {
    System.out.print((char)n);
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-09-21
2010-11-21 儿童唐诗三百首全集 453
相似回答