944,051 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1393
  • Java RSS
Nov 3rd, 2009
0

Merging two files

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  1. package mergetwofiles;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6.  
  7. public class Main {
  8.  
  9.  
  10. public static void main(String[] args) {
  11. FileReader file1=new FileReader("Data1.txt");
  12. FileReader file2=new FileReader("Data2.txt");
  13. BufferedReader br1 = new BufferedReader of(file1);
  14. BufferedReader br2 = new BufferedReader of(file2);
  15.  
  16. String temp1, temp2;
  17. while(br1.readLine() !=null)
  18. {
  19. temp1=br1.readLine()+temp1;
  20. }
  21. while(br2.readLine()!=null)
  22. {
  23. temp2=br2.readLine()+temp2;
  24. }
  25. String temp = temp1 + temp2;
  26. FileWriter fw=new FileWriter("data3.txt");
  27. char buffer[]=new char[temp.length];
  28. temp.getChars(0,temp.length(),buffer,0);
  29. fw.write(buffer);
  30. file1.close();
  31. file2.close();
  32. fw.close();
  33. }
  34. }
Similar Threads
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
StarZ is offline Offline
85 posts
since Dec 2008
Nov 3rd, 2009
0
Re: Merging two files
Click to Expand / Collapse  Quote originally posted by StarZ ...
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
Let me make sure I get what you said: I'm assuming that according to what you said, for the two files above, a third file would be produced:

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: mutlipication table in java program
Next Thread in Java Forum Timeline: Search array for account number





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC