RoboticPlayer RoboticPlayer -4 years ago 143
Java Question

How to get the BufferedWriter to write to a text file?

I'm trying to make an appointment book program in Java, however the BufferedWriter doesn't seem to be writing to the file.

This code is all inside of a

try { } catch (FileNotFoundException) { } catch (IOException) { }
block

FileWriter writer = new FileWriter("Appointments" + name + ".txt", true);
BufferedWriter bufferedWriter = new BufferedWriter(writer);

if (open == 'o') {
FileReader reader = new FileReader("Appointments" + name + ".txt");
BufferedReader bufferedReader = new BufferedReader(reader);
while ((line = bufferedReader.readLine()) != null) {
if (line.indexOf("one-time") == line.length() - 8) {
OneTime o = fromStringOneTime(line);
oneTimes.add(o);
}
if (line.indexOf("daily") == line.length() - 5) {
Daily d = fromStringDaily(line);
dailys.add(d);
}
if (line.indexOf("monthly") == line.length() - 7) {
Monthly m = fromStringMonthly(line);
monthlys.add(m);
}
}
}


The while loop is just initializing all of the values currently in the file into 3 different ArrayLists.

Then I have a block of code to create the new objects once you have selected that you want to add an appointment (variables
desc
and
date
have been previously defined):

System.out.print("Enter if this is a (o)ne-time, (d)aily, or (m)onthly appointment: ");
freq = scanner.next().charAt(0);
if (freq == 'o') {
OneTime o = new OneTime(desc, date.getDate());
oneTimes.add(o);
totalCount++;
bufferedWriter.write(o.toString());
bufferedWriter.newLine();
valid = true;
} else if (freq == 'd') {
Daily o = new Daily(desc, date.getDate());
dailys.add(o);
totalCount++;
bufferedWriter.write(o.toString());
bufferedWriter.newLine();
valid = true;
} else if (freq == 'm') {
Monthly o = new Monthly(desc, date.getDate());
monthlys.add(o);
totalCount++;
bufferedWriter.write(o.toString());
bufferedWriter.newLine();
valid = true;
}


At the end of the try block, I have the lines
reader.close()
and
writer.close()
. So why is it that when I check the file that it should be saving to, it still appears blank? This is my first time dealing with file saving, so I'm not sure why it isn't working the way it should be.

Edit: File will now only read if the option was chosen to open an existing file instead of creating a new one

Answer Source

Yes., Most of the operating system will not allow to read and write the same file. So you can go with the workaround by writing to a file with some temporary file name and rename it to original at the end of the program

try {
   FileWriter writer = new FileWriter("Appointments" + name + "1.txt", true);
   FileReader reader = new FileReader("Appointments" + name + ".txt");
   BufferedReader bufferedReader= new BufferedReader(reader);
   BufferedWriter bufferedWriter = new BufferedWriter(writer);
   .......
} catch () {
   ......
} finally {
   bufferedReader.close();
   bufferedWriter.flush();
   bufferedWriter.close();
}

// Rename the file and delete temp file
File srcFile = new File("Appointments" + name + "1.txt");
File targerFile = new File("Appointments" + name + ".txt");
srcFile.renameTo(targetFile);
srcFile.delete();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download