• Post Reply Bookmark Topic Watch Topic
  • New Topic

Combining Text Files

 
aarti reddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How to combine two text files into one text file??

Thanks in Advance,
BM.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
select java,

Welcome to Javaranch. We don't have too many rules around here but we do have a Naming Policy. Please read the policy and adjust your display name.

Thanks.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing you want to copy all of file 1 to a new output, then copy all of file 2 to the new output. You'll have to write the copy operation yourself - read from one file, write to another. Look into FileReader and FileWriter. Note that the docs have links to BufferedReader and BufferedWriter that will help you process one line at a time instead of just bytes. Take a stab at this - if you run into trouble post some code and somebody will help you out. Have fun! This is good stuff to learn.
 
aarti reddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan,
Actually i have two text files with a primary key,based on that primary key i need to merge two text files into one text file ,just like using SequenceInputStream class to combine two binary files into single binary file.Any help.

Thanks,
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java doesn't offer any special support for something like this -- anything you do will just have to be coded by hand. Read the first file into some kind of data structure, then read in the second file and add it to the same data structure, and then write the combined structure back out. I don't know what level of detail you need as I don't know what your skill level is.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, sequential merge! My favorite algorithm from my old COBOL days. This is common in merge and in master-update programs:

Write two lists of keys on a sheet of paper and walk through the algorithm to see how it works. Note that I didn't handle end of file here. One approach is to give end-of-file an impossibly high key. So if A goes to end, b.key will be less and we'll process the rest of b. When both go to end we exit the loop. A more pure approach (no bogus keys) is to modify the tests:

Hope that helps!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!