如何读取文件并按输出打印输出时间 [英] How DO YOU READ a FILE and PRINT OUT TIME as OUTPUT

查看:93
本文介绍了如何读取文件并按输出打印输出时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if(outputStream!= null){

outputStream.close();

}

}



}

else {

扫描仪linReader =新扫描仪(输入文件);

long start_time =系统。 currentTimeMillis();





while(linReader.hasNext())

{

String line = linReader.nextLine();

System.out.println(line);

}

linReader.close( );

long estimatedTime = System.currentTimeMillis() - start_time;

System.out.println(估计时间);









}



}

}

>



我尝试过:



如何将时间打印出来

解决方案

你的初始提示是m essage(s)在错误的地方,应该是:

 System.out.print( 输入输入文件名); 
String inputfile = k.next();
System.out.print( 输入输出文件名);
字符串 outputfile = k.next();



您还计算经过的时间在字符模式下处理文件时的每个字节应该是:

  int  c; 
while ((c = inputStream.read())!= - 1 ){
outputStream.write(c);
} // while循环应该在这里结束
long estimatedTime = System.currentTimeMillis() - start_time;
System.out.println(估计时间);



轻松发现1)正确缩进代码,2)仔细阅读你所写的内容。


if(outputStream != null){
outputStream.close();
}
}

}
else{
Scanner linReader = new Scanner(inputfile);
long start_time = System.currentTimeMillis();


while (linReader.hasNext())
{
String line = linReader.nextLine();
System.out.println(line);
}
linReader.close();
long estimatedTime = System.currentTimeMillis() - start_time;
System.out.println(estimatedTime);




}

}
}
>

What I have tried:

how do I will print it out the time

解决方案

You have your initial prompt message(s) in the wrong place, it should be:

System.out.print("Enter an input file name");
String inputfile = k.next();
System.out.print("Enter an output file name");
String outputfile = k.next();


You are also calculating the elapsed time for each byte when processing the file in character mode, it should be:

int c;
while((c = inputStream.read()) != -1) {
    outputStream.write(c);
} // while loop should end here
long estimatedTime = System.currentTimeMillis() - start_time;
System.out.println(estimatedTime);


Easily spotted by 1) indenting your code properly, and 2) reading carefully through what you have written.


这篇关于如何读取文件并按输出打印输出时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆