Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Merge Files

 
rose hegde
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me how to merge two files?
 
Joe Ess
Bartender
Posts: 9295
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First figure out how to open one file. Maybe write its contents to the console. Consult the Java Tutorial if you don't know how. Then figure out how to write a file out. Consult above tutorial. Then introduce the second file and the logic to compare the two inputs record by record, writing them in sorted order to the output file.
 
Vinod Venkatasubramanian
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use java.io.FileOutputStream.
It has a constructor with the signature
FileOutputStream(File file, boolean append) !
guess you can do the rest.
 
rose hegde
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i am new to know about merging files.So can u pls tell me from scratch?
Thanx
 
Joe Ess
Bartender
Posts: 9295
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't going to make me repeat myself, are you?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a common and age-old problem. In fact, I seem to type these few lines a lot of times here. I've been doing this since ... well, a long time.

This works on any two lists that are ordered on the same key, such as master file and updates or two purchased lists of e-mail addresses for spamming or whatever. I've used it with files, result sets, arrays, etc.
This doesn't show complete "end of file" logic. One trick is at end of one file set its key impossibly high. Then the comparisons will work out to process the rest of the other file. Another is to modify the tests to say "If B.eof or A.Key < B.Key ..."
We try not to give complete answers some times, but let you figure most of it out yourself. Let me know if that's enough detail, or going the right direction at all.
 
Maverick Lasa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rose hegde:
Can anyone tell me how to merge two files?

here's a very simple one.
remember that you have to catch the exceptions

it is not necessary that you have to use a buffer of 1 byte.
you may adjust its length and have to change the loop's condition
as well...
hope this helps.
mavedrive;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic