Hey, I know this is kind of old, but I have a very relevant question.. I want to make a program that stores data such as a high score in a text file. So far I have no problem with that, but when ever I re-run the program, that file it totally overridden. This sounds like what facultyofmusic was describing, but I don't know what to do to not do that. Here is some coding example:
Quote:
/* write data to file */
System.out.print("Enter the file name for storing names and grades: ");
fileName = input.nextLine();
System.out.print("How many students? ");
numStu = input.nextInt();
try {
dataFile = new File(fileName);
out = new FileWriter(dataFile);
writeFile = new BufferedWriter(out);
for (int i = 0; i < numStu; i++) {
System.out.print("Enter student name: ");
stuName = input.next();
System.out.print("Enter test score: ");
score = input.next();
writeFile.write(stuName);
writeFile.newLine();
writeFile.write(score);
writeFile.newLine();
}
writeFile.close();
out.close();
System.out.println("Data written to file.");
}
catch (IOException e) {
System.out.println("Problem writing to file.");
System.err.println("IOException: " + e.getMessage());
}
I assume most programmers can fill in the blanks, if not, I will paste the whole program. Thank you in advance for the help.
P.S. I am not assuming that the text file already exists. It needs to be created if it isn't already there (which is a simple if statement).
P.P.S. I have no idea what part of the code actually makes the file. I use <fileObject>.creatNewFile() but this program doesn't have that and it runs just fine. I got it from an example from my teacher who got it from a resource book.