File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Read from 2 files and write to the third file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Read from 2 files and write to the third file" Watch "Read from 2 files and write to the third file" New topic
Author

Read from 2 files and write to the third file

brad ryan
Greenhorn

Joined: Feb 13, 2002
Posts: 4
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Read from 2 files and write to the third file