• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

comparing the file before i append it to the other

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two files that need to be made into one file. The thing is file 1 has records A,B,C,D E and File 2 has records C,D,E,F,G,H. So now my program writes the final record as A,B,C,D,E,C,D,E,F,G,H.(some duplicate records) How can I get the file to just write the built file as ABCDEFGH.(each record is a line long) here is the program so far but I am confused about the compare part. any help would be great
import java.io.*;
import java.util.*;
public class MergeFile {
public static void main(String[] args) throws Exception {
File f1 = new File("C:\\Downloaded\\File.001");
File f2 = new File("C:\\Downloaded\\File.002");
File outFile = new File("C:\\Downloaded\\builtfile.001");
FileWriter outFlWr= new FileWriter(outFile);
BufferedWriter bufOutput = new BufferedWriter(outFlWr);
if(f1.exists() && f2.exists()) {
System.out.println("yes there are two files");
try {
FileReader inFlRd = new FileReader(f1);
BufferedReader bufInput = new BufferedReader(inFlRd);
String line;
line = bufInput.readLine();
while (line != null) {
bufOutput.write(line, 0, line.length());
bufOutput.newLine();
line = bufInput.readLine();
}//end while
bufInput.close();
}//end try
catch (IOException e) {
e.printStackTrace();
}
try {
FileReader inFlRd2 = new FileReader(f2);
BufferedReader bufInput = new BufferedReader(inFlRd2);
String line;
line = bufInput.readLine();
while (line != null) {
bufOutput.write(line, 0, line.length());
bufOutput.newLine();
line = bufInput.readLine();
}//end while
bufInput.close();
bufOutput.close();
}//end try
catch (IOException e) {
e.printStackTrace();
}
}//end if
else {
System.out.println("Process ran ran normal, only one PunchFile exists");
}
}//end main
}//end class

[This message has been edited by Tom Breuer (edited June 21, 2001).]
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
How do you distinguish the records? Is there a primary key or a unigue field in the record that you can use for comparisons? If so, I would read all the keys and make two separate lists of them. I would then discard the duplicate keys from the list that is from the file that is going to be appended, and then read through that file and append the records with those keys. This won't be too tough to do if you use RandomAccessFile. Hope this helps!
Cheers,
RL
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im sorry Im not experienced enough to understand, but I've never used RandomaccessFile. Do you know where there are any examples for what I need it for? thanks
 
Ryan Langley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
RandomAccessFile allows you to seek to a position in a file, say 5 characters/bytes after a newline character ('\n'). And then read x characters/bytes from that position. You could then seek to a different part of the same file, say 5 characters/bytes after the next newline character, and read some more characters/bytes. Unlike the other Readers/InputStreams it is not sequential access to a file, but random access to a file. You can access any part of the file at any time, as opposed starting from the beginning of the file and having to read characters/bytes to get to the part of the file you really want to read. Look it up in the javadocs. Here's a quick example that will read the 5th character of everyline and print it to screen:

------------------
Cheers,
RL
[This message has been edited by Ryan Langley (edited June 25, 2001).]
 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Tom! you really don't have to use RandomAccessFile if you're already comfortable with what you originally did. you just have to tell us the contents of each record so we can devise something that checks for duplicates. if you'll just going to be dealing with String Objects then that wouldn't be so hard...
[This message has been edited by Paul Michael Laborte (edited July 07, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic