Results 1 to 7 of 7
Thread: Logic Error: Not Writing To File
- 10-20-2008, 11:15 PM #1
Logic Error: Not Writing To File
I know I've been asking a lot of questions lately, but just bear with me if you can.
For some reason, my program is not writing to file like it should. Well, it's not writing to file at all. I receive no error; I notice it only when my program tries to read what it should have written. Here is the section of my program I believe is pertinent to this problem:
In Exercise.java:
In User.java:Java Code:/** * Handle button clicks. * @param e the event */ public void actionPerformed(ActionEvent e) { try { reader = new Scanner(new BufferedReader(new FileReader("exerciselog.txt"))); writer = new BufferedWriter(new FileWriter("exerciselog.txt")); // ... // Should read from the file else if(e.getSource().equals(showStatsButton)) { if(user != null) { user.readUser(userField.getText(), reader); } else { JOptionPane.showMessageDialog(null, "You must enter a User Name and submit it first."); } } // Should write to the file else if(e.getSource().equals(enterButton)) { user.addExercise(Integer.parseInt(timeField.getText()), dateField.getText(), typeField.getText(), writer); } } catch(FileNotFoundException ex) { Logger.getLogger(Exercise.class.getName()).log(Level.SEVERE, null, ex); } catch(IOException ex) { Logger.getLogger(Exercise.class.getName()).log(Level.SEVERE, null, ex); } finally { try { reader.close(); writer.close(); } catch(IOException ex) { Logger.getLogger(Exercise.class.getName()).log(Level.SEVERE, null, ex); } } }
Java Code:/** * Add a new exercise. * @param t the amount of time exercised in minutes * @param d the date of the exercise * @param e type of exercise * @param bw BufferedWriter to be used * @throws IOException */ public void addExercise(int t, String d, String e, BufferedWriter bw) throws IOException { time += t; bw.append(name + "::" + e + "::" + d + "::" + time + "::\n"); } /** * Read exercise information from a file. * @param n the name of the exerciser to be read * @param s the Scanner to be used * @throws IOException */ public void readUser(String n, Scanner s) throws IOException { name = n; s.useDelimiter("::"); s.next(n); type = s.next(); date = s.next(); time = Integer.parseInt(s.next()); JOptionPane.showMessageDialog(null, "Name: " + n + "\nExercise: " + type + "\nDate: " + date + "\nTime: " + time); }"Things are made of littler things that jiggle."
- 10-20-2008, 11:45 PM #2
Member
- Join Date
- Jul 2008
- Posts
- 68
- Rep Power
- 0
I'm not very familiar with BufferedWriter, but it seems from looking at the API doc that you should be calling one of the write methods of the BufferedWriter object to actually get it to write to the file.
- 10-21-2008, 12:58 AM #3
Have you tried debugging the code by adding println()s to show execution flow?
Why are you using the append() method?
- 10-21-2008, 01:41 AM #4
It was my understanding append() wrote to the end of the file (which is what I want). In case that wasn't the case, I tried write(); it gave the same error. That is, it didn't write to file.
I tried adding a println() statement to addExercise(), the method that should be writing. addExercise() is receiving the parameters correctly. I also added println() statements to the addExercise() call, to readUser(), and to the readUser() call. All println() statements print out correctly."Things are made of littler things that jiggle."
-
If you are in fact trying to write the end of a file, to append to an already existing file, do you need to change your constructor call on your FileWriter anonymous object? Do you need to add a boolean "true" parameter to this call (check the API for more details)?
- 10-21-2008, 02:03 AM #6
Member
- Join Date
- Jul 2008
- Posts
- 68
- Rep Power
- 0
What error did it give? append only appends things to the bufferedwriter, it doesn't actually write.
Check HERE to see what methods are available to the bufferedWriter
- 10-21-2008, 02:13 AM #7
Similar Threads
-
Writing to DAT or TXT file
By hunterbdb in forum Advanced JavaReplies: 7Last Post: 10-12-2008, 02:50 PM -
swapping the contents of the file and writing to another file
By Ms.Ranjan in forum New To JavaReplies: 9Last Post: 07-10-2008, 04:52 PM -
Writing to a file (at the end)
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:22 AM -
writing to a file
By bugger in forum New To JavaReplies: 1Last Post: 11-11-2007, 02:49 AM -
Help with File reading and writing
By baltimore in forum New To JavaReplies: 1Last Post: 07-31-2007, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks