| | |
Merging two files
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 65
Reputation:
Solved Threads: 1
I'm supposed to merge a ordered data of two files into a third file, keeping the data in order. I'm suppose to create a MergeFiles application that merges the integers ordered from low to high in two files into a third file, keeping the order from low to high. Then should merge the two files by taking one element at a time from each, and the third file should contain the numbers from both file from lowest to highest. so, i saved the numbers in wordpad as data1.txt, and data2.txt.
Data1: 11 25 36 45 56 78 90
Data2: 1 3 5 7 54 32 78 99
I can't compile my program because there are still errors and missing codes... can someone help me edit it, and is this how i do it according to the requirements. thx.
so far:
Data1: 11 25 36 45 56 78 90
Data2: 1 3 5 7 54 32 78 99
I can't compile my program because there are still errors and missing codes... can someone help me edit it, and is this how i do it according to the requirements. thx.
so far:
Java Syntax (Toggle Plain Text)
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(); } }
•
•
Join Date: Sep 2008
Posts: 1,657
Reputation:
Solved Threads: 206
0
#2 Nov 3rd, 2009
•
•
•
•
I'm supposed to merge a ordered data of two files into a third file, keeping the data in order. I'm suppose to create a MergeFiles application that merges the integers ordered from low to high in two files into a third file, keeping the order from low to high. Then should merge the two files by taking one element at a time from each, and the third file should contain the numbers from both file from lowest to highest. so, i saved the numbers in wordpad as data1.txt, and data2.txt.
Data1: 11 25 36 45 56 78 90
Data2: 1 3 5 7 54 32 78 99
data3: 1 3 5 7 11 25 32 36 45 56 78 78 90 99
So your only requirement is to order them from lowest to highest and put them in a third file? Or do you also have to read in the numbers in alternating fashion? Either way, since the order in which you read in the elements is trivial, I'll explain how this works.
Options:
1) Ordering your elements from lowest to highest is called sorting. To do so, you will need to use a sorting algorithm. So you could create an array (or ArrayList), read all of the numbers from both files in, and sort the array. In fact, if you do this using an ArrayList, sorting the numbers is as simple as calling the arrayList.sort() method. If you use an array you'll need to write your own sorting method.
2) You could also use a LinkedList to store your values. You would maintain the list's sorted order as you read in the values. To do so, you would figure out what place a particular value belonged in the LinkedList, then you would insert() it in that position. (This might actually be more efficient than option 1, but I'm not positive. Either way, both options work. The reason you'd use a LinkedList in this case is because if you're inserting new values between previous values (example, you currently have 15, 28 in your list and you read in 20) then you'd need to put 20 between 15 and 28 which in an array, would involve copying elements).
After you do one of those two things, you would simply write your list out to a file. To do file writing in Java I usually use PrintWriter . . I'm sure you can find other ways to do it (BufferedWriter, etc) and plenty of examples on google.
Last edited by BestJewSinceJC; Nov 3rd, 2009 at 8:51 pm.
Out.
![]() |
Similar Threads
- Merging 2 Files (C++)
- Getting size of all files in folder, and finding out some offsets.. (Python)
- merging two files and removing duplicates (Perl)
- Merging two files? (C)
Other Threads in the Java Forum
- Previous Thread: mutlipication table in java program
- Next Thread: Search array for account number
Views: 251 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation balls binary bluetooth business chat class classes client code codesnippet collections component coordinates database defaultmethod doctype dragging draw ebook eclipse educational error event exception file fractal game givemetehcodez graphics gui helpwithhomework hql html ide image ingres input integer invokingapacheantprogrammatically j2me java javaprojects jmf jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie number numbers object oracle parameter php print problem program programming project recursion scanner screen server set size sms socket sort sql string sun swing swt tcp test threads time transfer tree udp windows






