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 Java I/O for two file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Java I/O for two file" Watch "Java I/O for two file" New topic
Author

Java I/O for two file

Martin George
Greenhorn

Joined: Oct 30, 2000
Posts: 4
Help! Arrghh! This should be so simple.
I 'm trying to compare two files in several iterations.
The first file needs to be read by characters.
The second file should be read by lines.
The second file will have to be cycled through several times.
First Case (simplified code shown below):
I read in the second file and store in BufferedReader.
I mark file so I can go back to the top for another iteration.
I always get "java.io.IOException: Mark invalid"
How do I make this work!?
Second Case
So I try to Open/Process/Close...Open/Process/Close...
Still no go. The second file reads the first time.
It does not read the second time.
What am I missing?!?
Please Help!
mlg
/**/
public void methodIoa() {
String f1 = "D:\\file1.txt";
String f2 = "D:\\file2.txt";
String st1 = "", st2 = ""; int ctt = 0;
try {
FileReader fr1 = new FileReader(f1);
BufferedReader br1 = new BufferedReader(fr1);
FileReader fr2 = new FileReader(f2);
BufferedReader br2 = new BufferedReader(fr2);
//
while (!st1.equals("-1")) {
st1 = new Integer(br1.read()).toString();
System.out.println("\tst1:\t" + st1);
//*********************
if (ctt == 0) { br2.mark(1); } else { ctt++; }
while (st2 != null)
{
st2 = br2.readLine();
System.out.println("\tst2:\t" + st2);
}
br2.reset();
//*********************
}
br2.close(); fr2.close(); br1.close(); fr1.close();
System.out.println("Done");
}
catch (IOException e) { System.out.print(e); }
}
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
One think I see is your use of mark(1). The documentation on mark is
mark
public void mark(int´┐ŻreadAheadLimit)
throws IOException
Mark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.
Parameters: readAheadLimit - Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail. A limit value larger than the size of the input buffer will cause a new buffer to be allocated whose size is no smaller than limit. Therefore large values should be used with care.
Throws:
IllegalArgumentException - If readAheadLimit is < 0
IOException - If an I/O error occurs
Overrides: mark in class Reader
So, you have to ensure the buffersize is large enough to hold the entire contents of file2 and when you call set mark set it with a readahead limit equal to your buffer size.
One final thing I noted, your closing the same stream twice.
br2.close(); fr2.close(); br1.close(); fr1.close();
br2 and fr2 reference the same stream as does br1 and fr1. You only need to call the close on br1 and br2 or fr1 and fr2.
Hope this helps

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Martin George
Greenhorn

Joined: Oct 30, 2000
Posts: 4
It does. I thank you.
Martin George
Greenhorn

Joined: Oct 30, 2000
Posts: 4
I also note st2 gets defined as null later on in the file in some instances.
So, it must be "redefined" as "" (not nremain null) before the while loop.
- mlg
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java I/O for two file