0

I want to write a program that keeps what I've written in my file previously, and continually adds to it, instead of erasing it all every time I run the program.

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.Math;

class Movie_Ratings_2 {
public static void main(String[] args) {
Scanner n = new Scanner (System.in);
    String fileName = "output.txt";
                String x = n.nextLine();
        try {
            PrintWriter outputStream = new PrintWriter(fileName);
                outputStream.println(x);
                outputStream.close();
                outputStream.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
}

Seco01
  • 3
  • 4
  • 4
    Possible duplicate of [PrintWriter append method not appending](https://stackoverflow.com/questions/8210616/printwriter-append-method-not-appending) – luk2302 Sep 23 '19 at 19:59
  • 2
    The issue you're seeing is because you are using the `PrintWriter(String)` constructor. Looking at the [PrintWriter Javadoc for that constructor](https://docs.oracle.com/javase/8/docs/api/java/io/PrintWriter.html#PrintWriter-java.lang.String-): _If the file exists then it will be truncated to zero size; otherwise, a new file will be created._ – Kaan Sep 23 '19 at 20:06

0 Answers0