aspose file tools*
The moose likes Java in General and the fly likes tricky question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "tricky question" Watch "tricky question" New topic
Author

tricky question

Tom Breuer
Greenhorn

Joined: Jun 21, 2001
Posts: 10
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).]
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
hi,
here is how i would do:
i propose you to read the lines of the first file to a Vector (or thelike) first.
then read all the lines of the second file and compare if the vector already contains this line. if not: add it, if yes: drop it.
after that, you have a vector which contains unique lines of text and all you have to do is to write them back to another file.

i have my good day so i added some code :-) , this should work.
- the exception handling is not very apropriate: just one big tray/catch clause is bad style.....
- consider doing it static
- if you got huge files (some megs), then you get a very big vector.......this could slow donw the thing. perhaps ArrayList could help you there, it is faster (not synchronized)....
hope that helps and hope i did not confuse you :-)

dont go any further if you want to do it yourself ......................

<pre>
import java.io.*;
import java.util.*;
public class Demo {
/**
*
*/
static public void main(String[] args) {
Demo d = new Demo();
File f1 = new File("e:\\a.txt");
File f2 = new File("e:\\b.txt");
File f3 = new File("e:\\c.txt");
d.mergeFiles(f1, f2, f3);
}
public void mergeFiles(File f1, File f2, File destFile) {
try {
Vector temp = new Vector();
BufferedReader br;
String line;
// read the first file
br = new BufferedReader(new FileReader(f1));
line = br.readLine();
// read all lines of first file to a temporary vector
while (line != null) {
// add it to the vector
temp.add(line);
// read next line
line = br.readLine();
}//end while
// no we read the second file
br = new BufferedReader(new FileReader(f2));
line = br.readLine();
// read all lines of first file to a temporary vector
while (line != null) {
// is this line contained in the first file ?
if (!temp.contains(line)) {
// no, it isnt. lets add it to the vector
temp.add(line);
} else {
// just for demo.....this is a duplicate line
// do nothing
}
// read next line
line = br.readLine();
}//end while
// cleanup
br.close();
// at this point, the vector should contain unique lines. lets write it back to a file
PrintWriter pw = new PrintWriter(new FileWriter(destFile) ;
Enumeration en = temp.elements();
String outLine;
// enumerate through vector. (you could also use the indexes in a for loop...)
while (en.hasMoreElements()) {
// get nect element in vector
outLine = (String) en.nextElement();
// write the line
pw.println(outLine);
}
// dont forget to close writer
pw.close();
} catch(Exception e) {
System.out.println("Exception occured: "+e);
e.printStackTrace();
}
}
}
</pre>
karl
Tom Breuer
Greenhorn

Joined: Jun 21, 2001
Posts: 10
awesome reply, - never used a vector before, I was contemplating putting the last line of the File1 in a StringBuffer and comparing that one line to the second file, but your idea makes perfect sense. Now all I need to do is figure out some Runtime method to move the two original files to another directory using system commands for NT. like maybe run a .bat file. Anyway Thanks. TB
[This message has been edited by Tom Breuer (edited June 24, 2001).]
Tom Breuer
Greenhorn

Joined: Jun 21, 2001
Posts: 10
what do the switches actually do in /c /k ? Im thinking of the using Runtime.getRuntime().exec("cmd /K del File1.txt");
or Runtime.getRuntime().exec("cmd /K start fileMover.bat");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tricky question