Currently I am supposed to be writing a little java app for class. The app will consist of 3 classes: FileIO - Will do ALL file input and output tasks (open, close, read, write) LogParser - Will take data fed to it, and re-format it. Main - Will handle all data flow from FileIO to LogParser.
The way I plan to do this is as follows: 1) Main starts, creates a FileIO object with the name of the files to read and write. 2) FileIO opens the file. 3) Main tells FileIO to give the current line. FileIO returns a String. 4) Main determines if this an entry to format (String does not begin with #) 5) Main takes the LogString and hands it off to LogParser. 6) LogParser reformats (Scanner), and hands the reformatted String(Strings?) back to Main. 7) Main takes the reformatted String(Strings?) and passes it to FileIO. 8) FileIO writes the String(Strings?) to the output file.
Rinse, Lather, Repeat until all lines are done.
My problem is as follows: I need to keep the stream open, or remember what lines have been read so I do not repeat. Currently FileIO uses FileReader and BufferedReader to get the line, but the problem arises when I hand off the resulting String to main. If I return the String, the method from FileIO of course ends. However, when I need to get the next line FileIO must reopen the stream and read a line. However, a reopened stream will reset to the beginning...
You can split this between your main and your FileIO classes. Maybe FileIO opens the input in its constructor and has a method that reads and returns one line. It could say if the line is null, close the output. So main looks like:
If that makes sense, let's turn it around a bit. It's usually cool to tell an object what to do rather than ask it for data, so let's not get lines from FileIO, let's tell FileIO where to send them.
Now can you picture the loop in fileio reading each line and calling filter? See if that works for you. Sometime your class will talk about writing interfaces ... see if you could figure out how to use many different filters with the same fileio class.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I can do that, and in fact I did. However, the problem is main. It stands between the Parser and the FileIO classes. Your example only works if everything is in 1-2 classes. 3 classes, and it breaks. Or I am missing something.
It appears that you might be thinking you need to "hand off" all the output from main and give it to your parser, like handing a completed file folder to someone else. If that is the case, rethink the application -- consider creating a parser tool that you can instantiate inside of main, and have it process the file's strings as you go (within that while loop).