-2

How do i adjust the following code so that it will load the existing strings from file.txt and continue to add them in the same file without clearing the file on re-start?

ArrayList<String> ar = new ArrayList<String>();

void draw(){
}

void keyPressed() {
  if ((key=='1')) {
    String s1 ="Pressed one";
    ar.add(s1);
  }

  if ((key=='2')) {
    String s2 ="Pressed two";
    ar.add(s2);
  }

  if ((key == ENTER)) {
      String[] stringArray = ar.toArray(new String[0]);
      saveStrings("file.txt", stringArray);
      printArray(stringArray);
  }
}
Mitch_
  • 7
  • 3
  • You have two distinct question here. First you need a file reader to read the contents of a file and for the other you need a file writer object to write to the existing file. Have you tried to search this site for information on how to write to a file and keep the contents? I can guarentee both have been answered in various ways. – scrappedcola Oct 26 '16 at 18:38
  • @scrappedcola yes, i have searched the site and other forms as well. I even made a small sketch creating a text file using `createWriter` and `printWriter` saving the lines in the text file using `PrintWriter sum = createWriter("file.txt");` and `sum.println("pressed one");` but each time i run the sketch it re-creates the text file overwriting the existing one. – Mitch_ Oct 26 '16 at 18:46
  • http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java FileWrite with append set didn't work? Or how about FileWritter object with append set to true? For the reading the contents did you try a Scanner? – scrappedcola Oct 26 '16 at 19:55
  • I tried about everything today, I’m frustrated enough to ask for help here. The code i wrote is appending to an new line already but the point is that when i press ENTER or run the sketch again the .txt file clears itself. The content thats in the .txt file should stay in there no matter if i press ENTER 100 times or run the sketch again. – Mitch_ Oct 26 '16 at 20:38
  • What does `saveStrings` do? You don't present it so it's kind of a mystery as to what hitting the enter key entitles. – scrappedcola Oct 26 '16 at 20:48
  • The enter key converts the ArrayList to a StringArray, writes the Strings to a file using `saveStrings` and prints the Strings in the console. But it overwrites the .txt file. If you know a solution please share it. I will help me a lot. – Mitch_ Oct 26 '16 at 21:12
  • What I meant is you should put the code for saveStrings in your question. Right now I've given you lots to go on but you keep saying you tried it already. Rather than this pointless back and forth if you updated your question with details of what your current attempt at saving has you might get something worthwhile. You current sample above has nothing that would show how/why the file is being overwritten. – scrappedcola Oct 26 '16 at 21:15

1 Answers1

0

I think this is what you want:

PrintWriter output;
output=createWriter(*someDirectory*); //such as data/outputText.txt
String [] previousText = loadStrings (*someDirectory*) //same as above
String newText= "This is a new line that won't destroy entire text file";

for (int i=0; i<previousText.length; i++) {
  output.println(previousText[i]);
}

output.println(newText);
output.flush();
output.close();

Basically first you read the file and store the lines of the file to a String array. Then, first, you overwrite the file with this String array which is to say that you re-write everything. Then you add your new String and problem solved.