try{
FileInputStream fileInput=new FileInputStream(args[0]);
int data=fileInput.read();
int number=fileInput.available();
System.out.println(number);
while(data!=-1){
System.out.write(data);
data=fileInput.read();
}
//System.out.println("");
fileInput.close();
}
catch(IOException ioe){
System.err.println("Error"+ioe);
}
以上是程序。。
问题是每次读出文件内容的时候会读掉最后一行的内容,我想知道是为什么?另外我在输出内容的While循环后面加上任意一个输出(如注释中的),文件的最后一行又可以正常输出,是怎么回事?
FileInputStream fileInput=new FileInputStream(args[0]);
int data=fileInput.read();
int number=fileInput.available();
System.out.println(number);
while(data!=-1){
System.out.write(data);
data=fileInput.read();
}
//System.out.println("");
fileInput.close();
}
catch(IOException ioe){
System.err.println("Error"+ioe);
}
以上是程序。。
问题是每次读出文件内容的时候会读掉最后一行的内容,我想知道是为什么?另外我在输出内容的While循环后面加上任意一个输出(如注释中的),文件的最后一行又可以正常输出,是怎么回事?
out.flush();
}
---
上面是PrintStream中write(int b)中部分代码(System.out instanceof PrintStream = true)
可以看出当遇到\n才会将缓冲区的内容输出
读到最后一行的最后没有\n当然就不会输出了
可以在读完后调用System.out.flust()输出缓冲区里的内容
但是不知道是IE出了问题,还是CSDN,给分的时候提示说网页有错。。
等好了一定把分补上!