0

So I got this piece of code in my Java program;

String filename = "direct.txt";
String s = fil.getAbsolutePath();
Process p = Runtime.getRuntime().exec(s);
   try
   {
        PrintWriter outputStream = new PrintWriter(filename);
        outputStream.println(s);
        outputStream.close();
   }
   catch (FileNotFoundException e1) {e1.printStackTrace();};

But when it writes to the file, it overwrites it when it writes something new, how can I make it so it doesn't overwrite but instead goes to the next line and prints it there?

Compass
  • 5,867
  • 4
  • 30
  • 42
user3231227
  • 67
  • 1
  • 8
  • 2
    Already answered [there](http://stackoverflow.com/questions/8210616/printwriter-append-method-not-appending) – udalmik Feb 17 '14 at 17:53

1 Answers1

1

You could make a FileWriter object and pass it as an argument when making the PrintWriter object. That way if the file already exists then it won't be overwritten, but if it does not exist then it will be created. From there you can use the PrintWriter methods as normal:

FileWriter objectName = new FileWriter("filename", true);
PrintWriter outputStream = new PrintWriter(objectName);
Keyan P
  • 920
  • 12
  • 20