Fix writing on txt

Asked

Viewed 52 times

1

I have an excerpt of code that writes in TXT, it works but excludes what was written before, how do I make it nay replace what was already written?

Follow the excerpt of the 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();
        }
    }

1 answer

3

Add the Boolean true to the builder of PrintStream. 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 documentation of the PrintStream.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.