虚心请教是什么原因导致的错误

大志哥123 2007-08-18 10:31:04
工程中分别建立读写文件的类
public class ReadFile{

public static String read()
{
try{

File read = new File("c:\\djym.txt");
BufferedReader br = new BufferedReader(new FileReader(read));

String temp = null;
temp = br.readLine();
br.close();
return temp;

}
catch(FileNotFoundException e){ //文件未找到
System.out.println (e);
return null;
}
catch(IOException e){
System.out.println (e);
return null;
}
}
}

public class WriteFile{
public static void write(String str){
try{

File write = new File("c:\\djym.txt");

BufferedWriter bw = new BufferedWriter(
new FileWriter(write));


while(str != null){
//写文件
bw.write(str); //只适用Windows系统
}

bw.close();
return;


}
catch(FileNotFoundException e)
{ //文件未找到
System.out.println (e);
return;
}
catch(IOException e)
{
System.out.println (e);
return;
}
}
}
...全文
219 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
大志哥123 2007-08-18
  • 打赏
  • 举报
回复
ding
大志哥123 2007-08-18
  • 打赏
  • 举报
回复
使用时候
String djym = ReadFile.read();
int temp = new Integer(djym).intValue();
if(temp<10000||temp>=99999)
{
temp = 10000;
}
else
{
temp = temp+1;
}
WriteFile.write(new Integer(temp).toString());
我放在使用类的main函数中执行,却发现txt文件一直在写10001,文件越来越大
我的txt之前放了10000,请问为什么会一直做
wangyananlove 2007-08-18
  • 打赏
  • 举报
回复
顶楼上的
sslnchys 2007-08-18
  • 打赏
  • 举报
回复
while(str != null){
//写文件
bw.write(str); //只适用Windows系统
}
while改成if
saint13 2007-08-18
  • 打赏
  • 举报
回复
在你的main函数中,你获得的temp值应该是10001,
然后你使用WriteFile.write();

现在来看看write()代码.
while(str != null){
//写文件
bw.write(str); //只适用Windows系统
}
死循环,一直往文件中写入"10001"

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧