reading multiple text files

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 11
Reputation: r0n is an unknown quantity at this point 
Solved Threads: 0
r0n's Avatar
r0n r0n is offline Offline
Newbie Poster

reading multiple text files

 
0
  #1
Jun 25th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,000
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 147
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: reading multiple text files

 
0
  #2
Jun 25th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 11
Reputation: r0n is an unknown quantity at this point 
Solved Threads: 0
r0n's Avatar
r0n r0n is offline Offline
Newbie Poster

Re: reading multiple text files

 
0
  #3
Jun 26th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,000
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 147
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: reading multiple text files

 
0
  #4
Jun 26th, 2009
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).
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 114
Reputation: KirkPatrick is an unknown quantity at this point 
Solved Threads: 3
KirkPatrick KirkPatrick is offline Offline
Junior Poster

Re: reading multiple text files

 
0
  #5
Jun 26th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,000
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 147
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: reading multiple text files

 
0
  #6
Jun 26th, 2009
Yes, easy, just open them both and read them both inside the same loop.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 11
Reputation: r0n is an unknown quantity at this point 
Solved Threads: 0
r0n's Avatar
r0n r0n is offline Offline
Newbie Poster

Re: reading multiple text files

 
0
  #7
Jun 30th, 2009
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){}

}
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,000
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 147
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: reading multiple text files

 
0
  #8
Jun 30th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 74
Reputation: notuserfriendly is an unknown quantity at this point 
Solved Threads: 5
notuserfriendly notuserfriendly is offline Offline
Junior Poster in Training

Re: reading multiple text files

 
0
  #9
Jun 30th, 2009
you should use the scanner class imo.
more compact
http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 11
Reputation: r0n is an unknown quantity at this point 
Solved Threads: 0
r0n's Avatar
r0n r0n is offline Offline
Newbie Poster

Re: reading multiple text files

 
0
  #10
Jul 1st, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC