This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Java I/O for two file

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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); }
}
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Martin George
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does. I thank you.
 
Martin George
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic