打开文件是可以用添加模式
FileWriter writer =new FileWriter("file.txt",true)
                                             ~~~~~
最后的 true 表示写入的内容将被添加在原有文件之后

解决方案 »

  1.   

    try:StringBuffer txtConcent = new StringBuffer() ;
    String sSrcName="c:\\test.txt";
    txtConcent.append("abc\n") ;
    try
    {
    PrintWriter pw = new PrintWriter(new FileWriter(sSrcName, true), true);
    pw.println(txtConcent.toString());
    }
    catch (Exception e)
    {
    System.out.println("error");
    }
    }
      

  2.   

      public void writeToFile(String filename, String msg, boolean append){
        PrintWriter out;
        //String filename = filename;
          try{
            out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)), append);
            out.println(msg);
          }catch(IOException ioe){
            System.out.println(ioe);
          }
      }  writeToFile("Hello World!\n", "c:\\out.txt", true);