I am working on making a chatbot in java and I want it to be able to generate preferences(at random) on specific topics and write the preference to a file. For example, if I ask the chatbot "do you like cake?" and it has not yet been asked about "cake" before, it will generate a random preference(true or false). It then writes to a file("chatbotPreferences.txt" or something similar) its preference, which can be retrieved later if the subject of "cake" arises again. The main problem I am having with this is the file overwriting every time I run the program. So if I discuss "cake" with it, and it writes "cake=true" in the file, then I run it again and ask it about "pie" and "pie=false" might appear in the file, but "cake=true" is not listed.
Is there a way to prevent it from being overwritten or does anyone have any other ideas for doing this? Thank You
Asked
Active
Viewed 457 times
0

Knight_of_Ni
- 11
- 1
-
1Have you tried using a search engine with your question *verbatim*? This is asked so often, you shouldn't have problems finding it. – Polygnome Jul 29 '16 at 17:00
-
4go to this link [how-to-append-text-to-an-existing-file-in-java](http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) – kar09 Jul 29 '16 at 17:00
3 Answers
0
I don't know how you write the file, but using a FileWriter
, it's quite simple:
Pass true
as second parameter to the constructor.

Robert Kock
- 5,795
- 1
- 12
- 20
-
I'm not having trouble writing to the file. The problem is when I write a new preference, it overwrites everything else in the file. I can not add something without it deleting everything else. – Knight_of_Ni Jul 29 '16 at 17:03
-
-
As @Robert Koch said, don't use a PrintWriter. Use a FileWriter instead, and create it using append mode. – FredK Jul 29 '16 at 17:32
0
You could use Scanner
and a String
ArrayList, to read in the data like so:
import java.util.*;
Scanner in = new Scanner(new File("chatbotPreferences.txt"));
List<String> things = new ArrayList<String>();
while (in.hasNext())
things.add(in.nextLine());
in.close();
Then you could add things to your list using the add()
method in ArrayList, like so:
things.add("cake=true");
Finally, when it comes time to save, just read everything from your list. There are various ways to save (and it sounds like you've already done this), but here is an example:
import java.io.*;
try {
PrintWriter writer = new PrintWriter("chatbotPreferences.txt");
for (String s : things)
writer.println(s);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}

RobotKarel314
- 417
- 3
- 14
-
OK! Thank you all for the help. I think I can figure it out from here. :D – Knight_of_Ni Jul 29 '16 at 20:17
-
1Could you please accept the answer if it responds to your question? – Robert Kock Jul 29 '16 at 21:51
0
Like mentioned above you need to add true to the FileWrite constructor. Here is an example that shows it in action. If you run this code more than once it will add the text not overwrite.
import java.io.*;
public class FileWriting{
public static void main(String[] args)throws Exception{
FileWriter fw= new FileWriter("test.txt",true); //by adding true you can add to a pre-existing file
PrintWriter pw= new PrintWriter(fw);
pw.println("line 1");
pw.println("line 2");
pw.println("line 3");
pw.println("line 4");
pw.close();
FileReader fr= new FileReader("test.txt");
BufferedReader br= new BufferedReader(fr);
String x;
do{
x= br.readLine();
System.out.println(x);
}while(x!=null);
br.close();
}
}

monsterman
- 9
- 4