-2

I want to know how do I write a line to a file without clearing or flushing it. What classes and packages do I need? I've tried with FileOutputStream and PrintStream (using println) and with BufferedWriter and OutputStreamWriter (using write) but they erase the previous content from the file.

try
    {
        File txt = new File("marcos.txt");
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

        writer.write(registro);
        writer.newLine();
        writer.close();    

    }
    catch(Exception e)
    {
        System.out.println("Error en la escritura al archivo.");
    }

1 Answers1

0

You can use the method writeStringToFile under the class FileUtils from org.apache.commons.io

public static void writeStringToFile(File file, String data, Charset encoding, boolean append) throws IOException Writes a String to a file creating the file if it does not exist. Parameters: file - the file to write data - the content to write to the file encoding - the encoding to use, null means platform default append - if true, then the String will be added to the end of the file rather than overwriting Throws: IOException - in case of an I/O error

Eric
  • 193
  • 2
  • 8