Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Read from 2 files and write to the third file  RSS feed

brad ryan
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.util.*;
public class anntemp{

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

FileReader fr=new FileReader("C:\\Tom\\Ann.dat");
BufferedReader br=new BufferedReader(fr);
int couter=0;
while ((recAnn = br.readLine())!=null)
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"));
FileWriter fw=new FileWriter("C:\\Tom\\NewAnn.dat",true);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
StringBuffer writeValue = new StringBuffer();
}catch (Exception expe){
System.out.println("In Exception ");

Thank you all in advance,
Jim Yingst
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!