我有一个文本文件,其内容保存在我的计算机上。我想使用FileInputStream在控制台上显示字符,同时还要测量花费的时间。看起来像这样:
public class Readtime {
public static void main(String args[]) throws Exception{
FileInputStream in=new FileInputStream("bolbol.txt");
while(in.read()!=-1){
long startTime = System.nanoTime();
int x = in.read();
long endtime = System.nanoTime();
System.out.println(endtime-startTime);
System.out.println((char)x);
}
in.close();
}
}
我在控制台上得到的是以下内容:
8863
b
7464
d
6998
f
6997
h
现在其余的字母在哪里?好像只完成了4次读取操作。我的想法是朝char大小方向发展,一次read()
只能读取一个字节,但我什么也没得到。
while(in.read()!=-1){
long startTime = System.nanoTime();
int x=in.read();
您正在读取while条件中的数据并在循环中再次打印以读取
int i=0;
while((i=in.read())!=-1){
System.out.println((char)i);
}
您可以在此处查看官方文档
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句