工程中分别建立读写文件的类
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;
  } 
 } 
}

解决方案 »

  1.   

    使用时候
            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,请问为什么会一直做
      

  2.   

    在你的main函数中,你获得的temp值应该是10001,
    然后你使用WriteFile.write();现在来看看write()代码.
    while(str != null){
    //写文件
    bw.write(str); //只适用Windows系统
    }
    死循环,一直往文件中写入"10001"
      

  3.   

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