FileInputStream无法读取全部文件内容

小浣熊仔仔 2015-12-18 01:59:44
最近在学习java的输入流,遇到一些问题:
当用FileInputStream读取文件的时候,读到倒数第二行就停止(最后一行无法读取),用的是下面的代码。
我读取的文件如下:
注意:文件只有三行!!!!

在eclipse中的输出却是:

少了一行!!!!


实在找不出哪里错了,还请不吝赐教,感激不尽!



import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileReaderClass {
public static void main(String []args){
FileReaderClass fileReader = new FileReaderClass();

System.out.println("readByByte:");
fileReader.readByByte("c:\\file.txt");
}

private void readByByte(String fileName){
InputStream in = null;
try{
in = new FileInputStream(fileName);

int tem = -1;
while((tem = in.read()) != -1){
System.out.write(tem);
}
} catch(IOException e){
e.printStackTrace();
} finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

...全文
265 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
小浣熊仔仔 2015-12-18
  • 打赏
  • 举报
回复
引用 1 楼 rickylin86 的回复:
在while后面加上

System.out.flush();
就可以了
嗯,解决了,非常感谢。
小浣熊仔仔 2015-12-18
  • 打赏
  • 举报
回复
嗯,解决了,非常感谢。
rickylin86 2015-12-18
  • 打赏
  • 举报
回复
在while后面加上

System.out.flush();
就可以了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧