JAVA读写文件问题!

来源:百度知道 编辑:UC知道 时间:2024/05/25 19:30:55
我在读完文件内容后,把值传到另一个类里面调用:
File read = new File("d:\\data\\00846464.txt");
BufferedReader br = new BufferedReader( new FileReader(read));
String temp =br.readLine();
while(temp != null){
temp = br.readLine();
}
int rc = RPDLL.sendRequestEx(temp);
点击提交后,tomcat自动关闭,这是什么原因?请高手解答!
谢谢loweryou提醒,还真是死循环了!
那int rc = RPDLL.sendRequestEx(temp); 这样子传值应该是传不过去的吧?有什么办法能把所有读取到的内容全部传过去?

本人还没学Servlet,但是从你的代码看来,你的错误很明显。
String temp =br.readLine();
while(temp != null){
temp = br.readLine();
}
如果文件里有数据的话,将是一个死循环,即假如从文件里面取出了一条语句“hello”,temp就定了,不会再为空,死循环!!!服务器可能是受不了你了!!
你可以这样写试试看!
String temp =null;
while((temp=br.readLine()) != null){
temp = br.readLine();
}
这样就不会死循环了!

这样读文件肯定是错的
1.首先读文件的代码段要用trycatch包起来,这样有问题的话就知道错在哪了
2.其次读文件的内容也肯定有问题,你这样只读到了null
StringBuilder sb = new StringBuilder();
while(true){
try {
String temp = br.readLine();
if (temp == null) {
break;
}
sb.append(temp);
sb.append("\r\n");
} catch (IOException e) {
e.printStackTrace();
}
}
String str = new String(sb);
这样读出来的文件才对

你怎么就读一行呢,需要进行append,用StringBuffer来保存内容吧,你确定是真的死循环了?
while(temp != null){
temp = br.readLine();
}
这恶地方temp已经被重新赋值了,temp不会定的吧

tomcat自动关闭?这个貌似是J2SE的