2

How can i prevent prinwriter in java from overwriting what's inside of that particular file?

Ex. I have a student.txt file. I already have few names there. After running and modifying this How do I create a file and write to it in Java? whats inside of that file will be overwritten. I just want to add it to the new line.

Also, how can i possibly perform search?

Community
  • 1
  • 1
user3258603
  • 149
  • 2
  • 17

1 Answers1

6
PrintWriter out = new PrintWriter(new FileWriter("student.txt", true));

The true is the append parameter - which indicates whether the FileWriter should append to the file. If it was false it would overwrite existing data in the file.

What do you mean by "how can i possibly perform search"?

anycard
  • 313
  • 1
  • 11
  • tnx. this answers my first question. I mean how can i possibly search for a data in that file? – user3258603 Feb 18 '14 at 21:12
  • @user3258603 - Read the file, scan for the data. – Hot Licks Feb 18 '14 at 21:13
  • If you're looking for a string in the text file, you can use a Scanner object to read in data from the file and compare it to the string you are searching for. Can you be more specific... – anycard Feb 18 '14 at 21:15