import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class TestFile {
 public void outputDataToFile(int data) {  File file1 = new File("G:/mytxt.txt");
  if (!file1.exists()) {
   try {
    System.out.println("试图创建文件");
    file1.createNewFile();
   } catch (IOException e) {
    e.printStackTrace();
   }
  } else {
   System.out.println("文件已存在,直接输入");
   // 文件大于指定值时删除后重新创建
   if (file1.length() >= 10000000) {
    System.out.println("文件太大,试图重新创建文件");
    file1.delete();
    try {
     file1.createNewFile();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  FileWriter writer;
  try {
   writer = new FileWriter(file1);
   String dat = new String(new Double(data).toString());
   System.out.println(dat);
   // writer.append(" ");
   writer.append(dat);
   writer.flush();
   writer.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public static void main(String[] args) {  TestFile t = new TestFile();
  while (true) {
   try {
    Thread.sleep(300);
   } catch (InterruptedException e) {    e.printStackTrace();
   }
   int r = (int) (Math.random() * 1000);
   t.outputDataToFile(r);
  }
 }
}

解决方案 »

  1.   

    这是我修改后的代码,你看看吧! package test;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    public class TestFile {
     public void outputDataToFile(int data) {  File file1 = new File("G:/mytxt.txt");
      if (!file1.exists()) {
       try {
        System.out.println("试图创建文件");
        file1.createNewFile();
       } catch (IOException e) {
        e.printStackTrace();
       }
      } else {
       System.out.println("文件已存在,直接输入");
       // 文件大于指定值时删除后重新创建
       if (file1.length() >= 10000000) {
        System.out.println("文件太大,试图重新创建文件");
        file1.delete();
        try {
         file1.createNewFile();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
      }
      FileWriter writer;
      try {
      /**FileWriter
    public FileWriter(String fileName,
                      boolean append)
               throws IOException根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象。 参数:
    fileName - 一个字符串,表示与系统有关的文件名。
    append - 一个 boolean 值,如果为 true,则将数据写入文件末尾处,而不是写入文件开始处。 
    抛出: 
    IOException - 如果指定文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它
    */
       writer = new FileWriter(file1,true);
       String dat = new String(new Double(data).toString());
       System.out.println(dat);
       //writer.append(dat);
       writer.write(dat);
       writer.write(" ");
       writer.flush();
       //writer.close();
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
     public static void main(String[] args) {  TestFile t = new TestFile();
      while (true) {
       try {
        Thread.sleep(300);
       } catch (InterruptedException e) {    e.printStackTrace();
       }
       int r = (int) (Math.random() * 1000);
       t.outputDataToFile(r);
      }
     }
    }
      

  2.   

    writer = new FileWriter(file1, true);
    仔细看FileWriter的API解释。有一种是追加,有一种是覆写。
      

  3.   

    感谢1楼和2楼
    。我原来的想的是writer = new FileWriter(file1);再调用writer.append(dat);方法,结果在同一个进程内可以在文件末尾追加数据,但关闭程序重新开始的话,就会把文件中的内容清理掉,原来问题是在构造函数上!!!前面想了各种办法都不行,害得我调用dll来做。。再次感谢