Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending One File to Another in Java - Why Does My Code Overwrite?

Tags:

java

file

append

I'm a moderately-experienced C++ guy learning Java and having a great time learning on this website. Recently, I did some research on how Java can append one file to another. Suppose I have two files. Here's C:\tmp\permFile.txt:

Line A:   abcef
Line B:   ghijk
Line C:   lmnop

And here's C:\tmp\tmpFile.txt:

line 1:   1234567890
line 2:   0123456789
line 3:   9012345678
line 4:   8901234567

The objective here is simple; I want to append tmpFile.txt to the end of permFile.txt. I researched code to do this and thought I had the problem solved. Only today I noticed that permFile.txt looks like this after running the code:

Line A:   abcef
Line B:   ghijk
Line C:   lmnop:   9012345678
line 4:   8901234567

I think what is happening here is permFile is 48 characters long (16 chars/line). My code counts exactly 48 chars into tmpFile and appends from the 49th character on out. Weird, huh?

Here's the code:

import java.io.*;

public class myProgram {
   public static void main(String[] args) throws IOException {

      // Define two filenames:
      String permFile = "C:\tmp\permFile.txt";
      String tmpFile =  "C:\tmp\tmpFile.txt";

      appendFiles appender = new appendFiles(permFile, tmpFile);
      }
}

public class appendFiles {
    public static void appendFiles(String permFile, String tmpFile) throws IOException {
    // Appends file "tmpFile" to the end of "permFile"
    // Code adapted from:  https://www.daniweb.com/software-development/java/threads/44508/appending-two-java-text-files

        try {
        // create a writer for permFile
        BufferedWriter out = new BufferedWriter(new FileWriter(permFile, true));
        // create a reader for tmpFile
        BufferedReader in = new BufferedReader(new FileReader(tmpFile));
        String str;
        while ((str = in.readLine()) != null) {
            out.write(str);
            }
        in.close();
        out.close();
        } catch (IOException e) {
        }
    }
}

What I suspect I need to do is add a line which "points" the "out" BufferedWriter to the END of permFile, not the beginning. But my attempts to do this have been disastrous and not worth detailing here. Also, what I don't get is why the contents of tmpFile do not overwrite all of permFile if "out" is pointed at the start of permFile. Suspect I'm making a simple, rookie mistake.

Many thanks! -P

PS - Can't tell you how much I love this site. I always try to research my own solutions, but when you get stuck on a specific problem, there's no substitute to asking real people for help.

like image 536
Pete Avatar asked Oct 20 '22 21:10

Pete


2 Answers

Just add a newline character in your print like:

out.write("\n" + str);

This worked for me. The perm file contains after method runs:

Line A:   abcef
Line B:   ghijk
Line C:   lmnop
line 1:   1234567890
line 2:   0123456789
line 3:   9012345678
line 4:   8901234567
like image 183
brso05 Avatar answered Oct 22 '22 12:10

brso05


Add a out.newline(); before the loop and at the end of each iteration and it works.

out.newline();
while ((str = in.readLine()) != null) {
    out.write(str);
    out.newline();
}

Also minor nitpick, you should be printing the exceptions when you catch them otherwise they will go unnoticed.

like image 35
toddlermenot Avatar answered Oct 22 '22 10:10

toddlermenot