Results 1 to 3 of 3
  1. #1
    redapplesonly is offline Member
    Join Date
    Feb 2016
    Location
    New Jersey
    Posts
    7
    Rep Power
    0

    Default BufferedWriter Question... How to Append only to the end of the file?

    Hi everyone,

    (I am posting a similar post from a question I asked yesterday. The feedback from yesterday's post was excellent, and I was able to progress with my code. I've hit a new, follow-up problem, so apologies if these threads seem somewhat similar.)

    I want my larger program to spend all of its time writing output to a logfile. Originally, I defined a BufferedWriter at the beginning of main(), then ran the guts of my program, and then closed the BufferedWriter on the last line of main() as cleanup. But I wonder if this is the "cleanest" way to do this. After all, my program has a lot of moving parts and it may crash before completion. I may never reach that close() statement at the end. Also, what if I want to run the program indefinitely? Wouldn't it be better to just open and close the file only when I have something to write?

    With this in mind, I've read through the I/O tutorial on docs.oracle.com, plus spent the morning tinkering with what I think could be a viable solution. Suppose this is my program:

    Java Code:
    import java.io.BufferedWriter;
    import java.io.IOException;
    
    public class WriteAttempt {
    	
       public static void main(String[] args) throws IOException {
    
          FileManager myFileManager = new FileManager();        // Custom object to handle all file I/O, see below
    	
          BufferedWriter outWriter = null;
          myFileManager.SetUpFile(outWriter);
          myFileManager.AppendToFile(outWriter, "This is log entry #1.");
          myFileManager.AppendToFile(outWriter, "This is log entry #2.");
          myFileManager.AppendToFile(outWriter, "This is log entry #3.");
        }
    }
    And here is the FileManager object, where I want all my file I/O methods to reside:

    Java Code:
    import java.io.BufferedWriter;
    import java.io.OutputStreamWriter;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    class FileManager{
    
       public void FileManager(){
          // do nothing
       }
    
       public void SetUpFile(BufferedWriter outWriter){
       try {
          outWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("logfile.txt"), "utf-8"));
             outWriter.write("This is a superimportant log file.");
             outWriter.newLine();
          } catch (IOException x) {
             System.err.format("IOException: %s%n", x);
          } 
       try{
             outWriter.close();
          } catch (IOException x) {
             System.err.format("IOException: %s%n", x);
          }
       }
    	
       public void AppendToFile(BufferedWriter outWriter, String statement){
       try {
          outWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("logfile.txt"), "utf-8"));
             outWriter.append(statement);
             outWriter.newLine();
          } catch (IOException x) {
             System.err.format("IOException: %s%n", x);
          } 
       try{
             outWriter.close();
          } catch (IOException x) {
             System.err.format("IOException: %s%n", x);
          }
       }
    }
    In truth, my FileManager.SetUpFile() and FileManager.AppendToFile() are clones of one another, with the first using the "write" operation and the second using the "append" operation. And believe it or not, but I really struggled to write these simple methods with as few lines of code as possible. I'm still a Java newbie. :(

    Okay: So here's the problem. The above compiles just fine. When I execute it, however, this is the contents of the logfile:

    Java Code:
    This is log entry #3.
    I'm assuming that this means every time I call my FileManager.AppendToFile() method, the BufferedWriter opens the file and begins writing at the start of the file, not the end. I'm scratching my head over this, because that method is basically a glorified wrapper for the BufferedWriter's append() method, which I thought is supposed to, well, start writing at the end of the file. I'm clearly missing something critical, but for the life of me I can't figure out what that might be.

    Any thoughts? Any advice and/or criticism is extremely appreciated - I learn far more that way than any book or online tutorial.

    Many thanks!
    -P

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: BufferedWriter Question... How to Append only to the end of the file?

    Look at the API doc for the FileOutputStream class. One of its constructors has a parameter for appending to the file.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    redapplesonly is offline Member
    Join Date
    Feb 2016
    Location
    New Jersey
    Posts
    7
    Rep Power
    0

    Default Re: BufferedWriter Question... How to Append only to the end of the file?

    Ahhhhh...! You are a GENIUS sir! That fixed things perfectly. I could have spend months reading APIs and I wouldn't have found it.

    Thanks a billion!
    -P

Similar Threads

  1. append csv file?
    By innovo in forum New To Java
    Replies: 2
    Last Post: 07-28-2015, 10:00 AM
  2. Replies: 7
    Last Post: 07-02-2013, 01:59 AM
  3. Replies: 2
    Last Post: 04-02-2012, 01:59 PM
  4. Simple append question
    By Rageagainst20 in forum New To Java
    Replies: 0
    Last Post: 12-20-2007, 11:40 PM

Tags for this Thread

Posting Permissions

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