日期:2014-05-20  浏览次数:20709 次

关于java.io超级新手简单问题
import java.io.*;
public class TestFinally {
 public static void main(String args[]){
FileInputStream in=null;
try{
in =new FileInputStream("myfile.txt");
int b;
b=in.read();
while(b!=-1){
System.out.print((char)b);
b=in.read();
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
System.out.println(e.getMessage());
}
这段代码的红色部分为什么不能少,一少运行,CPU就占用99%??求高手详解....

------解决方案--------------------
探讨
那加上这句,不是重新定义了b吗?不会继续循环??

引用:

没有红色那句话,就剩:
while(b!=-1) {
System.out.print((char)b);
}

你不觉得这是个死循环么?CPU持续忙碌,当然负载很高。