| | |
reading multiple text files
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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!
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! 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...
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... •
•
Join Date: Apr 2008
Posts: 1,000
Reputation:
Solved Threads: 147
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).
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).
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){}
}
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){}
}
•
•
Join Date: Apr 2008
Posts: 1,000
Reputation:
Solved Threads: 147
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.
If so, open the output file before going into the loop.
•
•
Join Date: Jan 2007
Posts: 74
Reputation:
Solved Threads: 5
you should use the scanner class imo.
more compact
http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html
more compact
http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html
![]() |
Similar Threads
- reading/writing text files question - HELP! (Python)
- searching for keywords in multiple text files (C++)
- reading contents of text files held in an array (C++)
- C++ Extracting data from text files (C++)
- Concactinating Multiple Text Files Into One (Java)
- Need help in converting multiple text files into excel worksheets in a workbook (Shell Scripting)
- Need reference to Random Accessing of Text files (C++)
- Java multiple text file saving (Java)
Other Threads in the Java Forum
- Previous Thread: Homework help increasing/decreasing days of the week & a clock
- Next Thread: how to be a java programmer
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api append apple applet application arguments array arrays automation bi binary bluetooth businessintelligence busy_handler(null) chat class classes client code component database draw eclipse encryption equation error event exception fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop main map method methods mobile netbeans newbie number oracle oriented panel print problem program programming project properties qt recursion reference replaysolutions repositories return robot scanner screen scrollbar se server set singleton size sms socket sort sql string swing test threads time tree utility windows xor xstream






