Results 1 to 7 of 7
  1. #1
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Default 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:
    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);
          }
        }
      }
    In User.java:
    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."

  2. #2
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    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.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Have you tried debugging the code by adding println()s to show execution flow?

    Why are you using the append() method?

  4. #4
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Default

    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."

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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)?

  6. #6
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    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

  7. #7
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Default

    Ah! Thank you, Fubarable! I added the "true" parameter to the FileWriter constructor, and now it works.
    "Things are made of littler things that jiggle."

Similar Threads

  1. Writing to DAT or TXT file
    By hunterbdb in forum Advanced Java
    Replies: 7
    Last Post: 10-12-2008, 02:50 PM
  2. Replies: 9
    Last Post: 07-10-2008, 04:52 PM
  3. Writing to a file (at the end)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:22 AM
  4. writing to a file
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-11-2007, 02:49 AM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •