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!

Recommended Answers

All 9 Replies

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?

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...

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).

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

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

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){}

        }

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.

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.