public void appendToFile(String str, String filename) throws Exception 
   {
      // Open up an outputstream writer to the file, and append str to it.
      FileOutputStream stream;//provides file access
      OutputStreamWriter writer;//writes to the file
      try 
      {
         stream = new FileOutputStream(filename, true);
         writer = new OutputStreamWriter(stream);
         writer.write(str);
         writer.close();
         stream.close();
      }//try
      catch(Exception e) 
      {
         throw e;
      }//catch
   }//appendToFile

解决方案 »

  1.   

    import java.io.*;public class TestWrite {

    public static void main(String[] args) {

    try {
    FileReader fr  =  new FileReader( "c:\\aaa.txt" );
    String oldCon = "";
    int c;
    while ( (c = fr.read()) != -1 ) 
    oldCon += (char)c;

    fr.close();

    FileWriter fw = new FileWriter( "c:\\aaa.txt" );
    fw.write(oldCon);
    fw.write("insert to the file end.");
    fw.close();
    } catch (Exception e) {

           }
    }
    }
      

  2.   

    使用构件器:FileOutputStream(String name, boolean append),置布尔值为ture。