我现在想向一个文件中添加内容,每次添加一行,我现在的做法如下:
            FileWriter fileWriter = new FileWriter(new File(logPath));
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);            bufferedWriter.write("HelloYG");            bufferedWriter.newLine();            bufferedWriter.close();
但是这样写入之后,第二次启动程序并且运行的时候,上次写的内容就会丢失,请教各位我应该
如何保持住每次写入的内容,就是说每次只是添加内容!

解决方案 »

  1.   

    FileWriter fileWriter = new FileWriter(new File(logPath),true);
    就可以了
      

  2.   

    FileOutputStream
    public FileOutputStream(String name,
                            boolean append)
                     throws FileNotFoundExceptionCreates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. 
    First, if there is a security manager, its checkWrite method is called with name as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown. 
    Parameters:
    name - the system-dependent file name
    append - if true, then bytes will be written to the end of the file rather than the beginning 
    Throws: 
    FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason. 
    SecurityException - if a security manager exists and its checkWrite method denies write access to the file.
    Since: 
    JDK1.1 
    See Also:
    SecurityManager.checkWrite(java.lang.String)