只要调用下面的方法,CPU一下就飙升到100% ,文件存在,不知道什么问题。且这文件只有一行不到10位的数字,应该不会是溢出什么的吧。代码中有一处判断文件是否存在的,输出证明此文件是存在的。代码如下:public static String readfile(String url){
try{
String filecontent="";
String temp=null;
File readfile=new File(url);
if (readfile.exists()){
System.out.println("ok:"+url);
}
BufferedReader read=new BufferedReader(new FileReader(readfile));
temp=read.readLine();
while(temp!=null){ //
temp=temp+"\n";
temp=temp+read.readLine();
}
read.close();
filecontent=temp;
return filecontent;
}
catch(Exception e){
e.printStackTrace();
return "";
}
}
try{
String filecontent="";
String temp=null;
File readfile=new File(url);
if (readfile.exists()){
System.out.println("ok:"+url);
}
BufferedReader read=new BufferedReader(new FileReader(readfile));
temp=read.readLine();
while(temp!=null){ //
temp=temp+"\n";
temp=temp+read.readLine();
}
read.close();
filecontent=temp;
return filecontent;
}
catch(Exception e){
e.printStackTrace();
return "";
}
}
temp=temp+"\n";
temp=temp+read.readLine();
}这个有问题,只要有一行数据,temp永远不可能为空,死循环了,temp=read.readLine();
temp=temp+"\n";
temp=temp+read.readLine();
} 木有明白你这么写干啥。 分析楼上的已经说了
String result="";
while(temp!=null){ //
result=temp+"\n";
temp=read.readLine();
}
return result;
这样不会死循环了吧