Forums Login/signup

Read from 2 files and write to the third file

+Pie Number of slices to send: Send
Hi all,
Please suggest me where i'm goin goin wrng.
I have 2 flat files. one of them is the main file(Ann.dat) has a about 150,000 lines (each line has unique ID from 00001 to 45000) of data and the the other(Miss.dat) has a just a list of IDs that are no longer in use & have to be deleted from the first file(NewAnn.dat). (Note that Ann.dat is a tab delimitted file and Miss.dat is just a list of all invalid IDs)
Below is my code. It doesn't do what I'm supposed to. Please suggest me or help me with a code to do it. What I'm trying to do is read each of the lines from the 2 files compare the ID in ann.dat with all the IDs in Miss.dat & if it doesn't match with the ID in Miss.dat write the whole line to NewAnn.dat. And do the rest with all the lines in Ann.dat.
It could be a real dumb question. since i'm not a software professional, I consider myself to be newbie to programming. I desperately need your help.

import java.io.*;
import java.util.*;
public class anntemp{

public static void main(String[] args)
{
String keyAnn ="";
String keyMis="";
String recAnn =null;
String recMis =null;

try{
FileReader fr=new FileReader("C:\\Tom\\Ann.dat");
BufferedReader br=new BufferedReader(fr);
int couter=0;
while ((recAnn = br.readLine())!=null)
{
couter++;
keyAnn = recAnn.substring(0, recAnn.indexOf("\t"));
FileReader fr1=new FileReader("C:\\Tom\\Miss.dat");
BufferedReader br1=new BufferedReader(fr1);
while((recMis = br1.readLine())!=null){
keyMis = recMis.substring(0, recMis.indexOf("\t"));
if(keyAnn.equals(keyMis)){
FileWriter fw=new FileWriter("C:\\Tom\\NewAnn.dat",true);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
StringBuffer writeValue = new StringBuffer();
writeValue.append(recAnn);
pw.println(writeValue.toString());
pw.flush();
}
}
}
}catch (Exception expe){
System.out.println("In Exception ");
expe.printStackTrace();
}
}
}

Thank you all in advance,
br
+Pie Number of slices to send: Send
Well, what about putting in some print statements to get feedback on what your variables are? Just after you read a line, put

and just after you parse a key, put

Et cetera. This way you can see if everything looks like you expect it to. If so, you can add more print statements for things like

Eventually you should find some part that's not behaving the way you expect - and by inspecting that part carefully, you can gradually figure out what the problem is.
[ August 08, 2002: Message edited by: Jim Yingst ]

reply
reply
This thread has been viewed 1628 times.
Similar Threads
FileIO
Create .txt file
close() method; I/O class chaining
Writing to File
comparing two files
More...

All times above are in ranch (not your local) time.
The current ranch time is
Jun 28, 2025 06:56:36.