Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,360 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,705 people online right now. Registration is fast and FREE... Join Now!




Merging .txt files?

 

Merging .txt files?

Runesmith

28 Apr, 2008 - 07:30 PM
Post #1

New D.I.C Head
*

Joined: 7 Mar, 2008
Posts: 15

Is it possible to merge two (or more) text files together using Java, and if so, how would it be done? I really don't want to have to resort to copy and pasting everything over as both files are quite large, but I haven't been able to find any code or information about it on the internet.

For example, assume the first text file looks like this:

A1
B1
C1

And the second looks like this:

2
3
4

I'd like to merge them together to look something like this:

A12
B13
C14


User is offlineProfile CardPM
+Quote Post


alpha02

RE: Merging .txt Files?

29 Apr, 2008 - 08:09 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 688



Thanked: 5 times
Dream Kudos: 850
My Contributions
To point you toward the right direction, you could open two input streams (one for each text file), read a line of the first one, then a line from the second one, concat them together and print the result in the new text file using a print stream.
User is offlineProfile CardPM
+Quote Post

Runesmith

RE: Merging .txt Files?

30 Apr, 2008 - 09:05 PM
Post #3

New D.I.C Head
*

Joined: 7 Mar, 2008
Posts: 15

QUOTE(alpha02 @ 29 Apr, 2008 - 09:09 AM) *

To point you toward the right direction, you could open two input streams (one for each text file), read a line of the first one, then a line from the second one, concat them together and print the result in the new text file using a print stream.


Something like this? The code isn't compiling correctly, but I think I'm on the right track. wink2.gif Any help would be appreciated!

CODE

package mergetwofiles;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;

public class Main {

    
    public static void main(String[] args) {
        FileReader file1=new FileReader("Data1.txt");
        FileReader file2=new FileReader("Data2.txt");
        BufferedReader br1 = new BufferedReader of(file1);
        BufferedReader br2 = new BufferedReader of(file2);
        
        while(br1.readLine() !=null)
        {
        String temp1=br1.readLine()+temp1;
        }
        while(br2.readLine()!=null)
        {
        String temp2=br2.readLine()+temp2;
        }
        String temp = temp1 + temp2;
        FileWriter fw=new FileWriter("file3.doc");
        char buffer[]=new char[temp.length];
        temp.getChars(0,temp.length(),buffer,0);
        fw.write(buffer);
        file1.close();
        file2.close();
        fw.close();
      }

}

User is offlineProfile CardPM
+Quote Post

pbl

RE: Merging .txt Files?

30 Apr, 2008 - 09:16 PM
Post #4

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,533



Thanked: 1124 times
Dream Kudos: 450
My Contributions
[You are on the right track indeed....

But you have to remove your definition of temp1 and temp2 out of the while so they will be seeen out of he loop

CODE

        String temp1, temp2;
        while(br1.readLine() !=null)
        {
        temp1=br1.readLine()+temp1;
        }
        while(br2.readLine()!=null)
        {
        temp2=br2.readLine()+temp2;
        }
        String temp = temp1 + temp2;



and also
User is offlineProfile CardPM
+Quote Post

Runesmith

RE: Merging .txt Files?

1 May, 2008 - 10:56 AM
Post #5

New D.I.C Head
*

Joined: 7 Mar, 2008
Posts: 15

Thanks, PBL! That took away some of the errors. I'm still getting errors around the BufferedReader lines ("Can't find ( or ]") and the temp.length line (It's highlighting .length, and saying it 'can't find symbol'), and I don't quite understand why this is happening. Here's my code at the moment:

CODE

package mergetwofiles;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;

public class Main {

    
    public static void main(String[] args) {
        FileReader file1=new FileReader("Data1.txt");
        FileReader file2=new FileReader("Data2.txt");
        BufferedReader br1 = new BufferedReader of(file1);
        BufferedReader br2 = new BufferedReader of(file2);
        
        String temp1, temp2;
        while(br1.readLine() !=null)
        {
        temp1=br1.readLine()+temp1;
        }
        while(br2.readLine()!=null)
        {
        temp2=br2.readLine()+temp2;
        }
        String temp = temp1 + temp2;
        FileWriter fw=new FileWriter("data3.txt");
        char buffer[]=new char[temp.length];
        temp.getChars(0,temp.length(),buffer,0);
        fw.write(buffer);
        file1.close();
        file2.close();
        fw.close();
     }
}

User is offlineProfile CardPM
+Quote Post

pbl

RE: Merging .txt Files?

1 May, 2008 - 11:33 AM
Post #6

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,533



Thanked: 1124 times
Dream Kudos: 450
My Contributions
QUOTE(Runesmith @ 1 May, 2008 - 11:56 AM) *

Thanks, PBL! That took away some of the errors. I'm still getting errors around the BufferedReader lines ("Can't find ( or ]") and the temp.length line (It's highlighting .length, and saying it 'can't find symbol'), and I don't quite understand why this is happening. Here's my code at the moment:


What you are doing is reading all file1 then all file2 cumulating them in temp1 and temp2
that is not what you want, you want it line per line
3 typos and unhandled exception
FileWriter has a method to write a String no need to convert into a char[]
This should do the trick

java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;

public class Main {


public static void main(String[] args) {
try {
FileReader file1=new FileReader("Data1.txt");
FileReader file2=new FileReader("Data2.txt");
BufferedReader br1 = new BufferedReader(file1); // remove the "of"
BufferedReader br2 = new BufferedReader(file2); // removed the "of"
FileWriter fw=new FileWriter("data3.txt");

String temp1, temp2;
temp1 = br1.readLine();
while(temp1 != null) {
temp2 = br2.readLine();
fw.write(temp1 + temp2);
// read next line
temp1 = br1.readLine();
}
file1.close();
file2.close();
fw.close();
}
catch (Exception e) {
System.out.println("Error opening/reading/writing file: " + e);
}

}
}


Have fun... I am not sure about the write(String) method in a FileWriter may be (I said MAY BE) you have to add a new line at the end of the line so it would be

fw.write(temp1 + temp2 + "\n");


This post has been edited by pbl: 1 May, 2008 - 11:39 AM
User is offlineProfile CardPM
+Quote Post

pratik.96

RE: Merging .txt Files?

3 Jul, 2009 - 10:52 PM
Post #7

New D.I.C Head
*

Joined: 3 Jul, 2009
Posts: 1

Hey i hv used your codes but i found errors in them......
so i hv corrected them n the code is
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
public class main1 {
public static void main(String[] args) {
try {
FileReader f1=new FileReader("b.txt");
FileReader f2=new FileReader("c.txt");
BufferedReader br1 = new BufferedReader(f1);
BufferedReader br2 = new BufferedReader(f2);
FileWriter x=new FileWriter("d.txt");
String a, b;
a = br1.readLine();
while(a != null) {
b = br2.readLine();
x.write(a + b+" ");
a = br1.readLine();
}

f1.close();
f2.close();
x.close(); }
catch (Exception e) {
System.out.println("Error opening/reading/writing file: " + e);
}
}
}

Plz anyone help me to Get the output in Column wise..............
Reply as sonn as possible........

This post has been edited by pratik.96: 3 Jul, 2009 - 10:55 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 07:50PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month