Correct writing in txt

1

I have a code snippet that writes in TXT, it works however it deletes what was written before, how do I make it not replace what was already written?

Here's the snippet of code I'm using:

public class TestandoEscrita {
    public static void main(String[] args) throws FileNotFoundException {
            PrintStream out = new PrintStream("c:/EscritaUrna.txt");
            Locale locale = new Locale("pt","BR");
            GregorianCalendar calendar = new GregorianCalendar(); 
            out.println("Voto Computado Dia");

            SimpleDateFormat formatador = new SimpleDateFormat("dd' de 'MMMMM' de 'yyyy' - 'HH':'mm'h'",locale);

            out.println(formatador.format(calendar.getTime()));
            out.close();
        }
    }
    
asked by anonymous 16.10.2017 / 20:22

1 answer

3

Add Boolean true to PrintStream constructor. Ex:

public class TestandoEscrita {
    public static void main(String[] args) throws FileNotFoundException {
            PrintStream out = new PrintStream("c:/EscritaUrna.txt", true); // <- aqui
            Locale locale = new Locale("pt","BR");
            GregorianCalendar calendar = new GregorianCalendar(); 
            out.println("Voto Computado Dia");

            SimpleDateFormat formatador = new SimpleDateFormat("dd' de 'MMMMM' de 'yyyy' - 'HH':'mm'h'",locale);

            out.println(formatador.format(calendar.getTime()));
            out.close();

        }
    }

When you make explicit the true in the constructor everything that is written goes to the end of the file and does not overlap with the data already recorded. See more in the PrintStream .

    
16.10.2017 / 20:27