954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

reading multiple text files

hi everyone

how should i move the data of the multiple text file to another text file...

(for instance you have two text files
readFile1.txt and readFile2.txt
and you have to move the data of the two text files
in another file which is writeFile.txt )

how should i read the two text files at the same time and write it to another text file?..

any idea?..

all i know is reading and moving one file at a time using this method
BufferedReader br = new BufferedReader(new FileReader(read.txt))
PrintWriter pw= new PrintWriter(new FileWriter(write.txt))

:) thanks!

r0n
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

How are the tw files to be combined? One after the other, alternating lines from each file, append each line from one file to the end of each line from the other, or what?

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

actually my issue here is if, i write readFile.txt in a textbox it will scan if there is any readFile in the directory even the file name is readFile1.txt and readFile2.txt
and put all the records in the writeFile.txt and combining the data is one after the another...

the BufferedReader and the PrintWriter is the method that i know in reading and writing the data, one at a time...

any help..

:) thanks...

r0n
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

Check out the File class. You can use that to get a list of all the files in a directory. You can then scan the file names to see if they match your criteria. Each time you find a file with a matching name you can open a BufferedReader for that file and read in its contents into the textbox.
Once you've got all the data from all the files in your textbox you can open a PrintWriter to the readFile.txt file and print all the data to it (this will replace the previous contents).

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Not to hijack this thread, but is there a way to do what his original question is asking? Reading two files at the same time? The reason I ask is because I am wanting to compare two text files and then write the new information to one file

KirkPatrick
Junior Poster
162 posts since Apr 2009
Reputation Points: 38
Solved Threads: 8
 

Yes, easy, just open them both and read them both inside the same loop.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

i did that!
thanks for the tip!

but i do have another issue here regarding, writing the data into the text file.

i cant display all the output in my writeFile.txt..
but whenever i display the output in the console it runs smoothly

by the way here's my code
i cant find the error here...

:) any help thanks..

String s[] = file.list(FilternameFilter);


String writeFile="writeFile.txt";
String line;

for (int i = 0; i< s.length; i++){


try{
BufferedReader br = new BufferedReader(new FileReader(s[i]));
PrintWriter pw = new PrintWriter (new FileWriter(writeFile));

while((line=br.readLine())!=null){

//only display the data of the readFile2
pw.println(displayLine);

//display the data of the readFile1 and readFile2
System.out.println(displayLine);

}

pw.close();
br.close();


}catch(IOException ie){}

}

r0n
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

Looks like you open the output file inside the loop, so you open it again for each file you read. When you re-open it it will overwrite whatever was there before, so I expect you will only see the very last file printed to writeFile, and all the earlier ones will be lost.
If so, open the output file before going into the loop.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

you should use the scanner class imo.
more compact
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

notuserfriendly
Junior Poster
104 posts since Jan 2007
Reputation Points: 11
Solved Threads: 7
 

oh...well i just figure it out...

james your right,...loop issues...heheheh..

:) thanks for the advice!

and thanks everyone for leaving a comment in this thread!:twisted:

r0n
Newbie Poster
19 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You