wood burning stoves 2.0*
The moose likes Java in General and the fly likes Merge Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Merge Files" Watch "Merge Files" New topic
Author

Merge Files

rose hegde
Ranch Hand

Joined: Oct 22, 2003
Posts: 40
Can anyone tell me how to merge two files?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Vinod Venkatasubramanian
Greenhorn

Joined: Oct 19, 2003
Posts: 13
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

Joined: Oct 22, 2003
Posts: 40
Actually i am new to know about merging files.So can u pls tell me from scratch?
Thanx
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

You aren't going to make me repeat myself, are you?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Maverick Lasa
Greenhorn

Joined: Oct 18, 2003
Posts: 23
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;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Merge Files