Results 1 to 3 of 3
  1. #1
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default Writing to a file (at the end)

    In my program, every time that you run the program, it creates a block of text in a specified text file. The problem is that when it runs, it just overwrites the last block. I tried using Java Tip's code (that he put in a thread titled the same as mine), but I didn't know how to get it to work, as it was surprisingly long.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    every time that you run the program, it creates a block of text in a specified text file. The problem is that when it runs, it just overwrites the last block.
    Some general approaches to this are:
    1 read the file, save the text as you read it, append the new text that you want to write to the file for this session and write all of the text back to the file
    2 use a RandomAccessFile. The javadoc and the tutorial have more about this special class and its use
    3 use a FileWriter which has a constructor with a boolean for appending text. Possible limitation: FileWriter is a convenience class for writing character files.

    I tried using Java Tip's code (that he put in a thread titled the same as mine), but I didn't know how to get it to work, as it was surprisingly long.
    Java Tip made use of RandomAccessFile in the AppendFileStream class in a clever way. As for the length the class extends the abstract OutputStream class which has 5 methods. The last method (public void write(int b)) is abstract so it must be implemented in/by subclasses. Java Tip implemented 4 of the 5 methods declared in OutputStream (all but the flush method) to direct the writing to the RandomAccesFile.

    How to use it is demonstrated in the appendtext class:
    Java Code:
        PrintStream out = 
            new PrintStream(new AppendFileStream("myfile"));
        out.print("A new line of text");
        out.close();
    An example using Java Tip's AppendFileStream class:
    Java Code:
    import java.io.*;
    
    public class AppendToFileTest {
        public static void main(String[] args) {
            String path = "appendToFileTest.txt";
            String[] testStrs = {
                "line 1", "line 2", "line 3", "line 4"
            };
            // Since the AppendFileStream class declaration
            // advertises that it throws IOExceptions we must
            // either throw or catch IOExceptions to use the class.
            try {
                PrintStream out = new PrintStream(
                                  new AppendFileStream(path));
                for(int i = 0; i < testStrs.length; i++) {
                    out.println();
                    out.print(testStrs[i]);
                }
                out.close();
            } catch(IOException e) {
                System.out.println("write error: " + e.getMessage());
            }
        }
    }

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I believe using the FileOutputStream(File f, boolean append) constructor will work. The API docs say that it appends to the end of the file. Haven't tested it though...
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Writing to a .txt File
    By ngc0202 in forum New To Java
    Replies: 7
    Last Post: 07-26-2009, 03:18 AM
  2. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  3. Writing to DAT or TXT file
    By hunterbdb in forum Advanced Java
    Replies: 7
    Last Post: 10-12-2008, 02:50 PM
  4. Replies: 9
    Last Post: 07-10-2008, 04:52 PM
  5. writing to a file
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-11-2007, 02:49 AM

Posting Permissions

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