Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dealing with Streams  RSS feed

 
Patrick Sylvester-Jose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

Gah!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some place you need something like this:

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.
 
Patrick Sylvester-Jose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Kiley
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

Don't know if that helps, but what the heck.

jk
 
Patrick Sylvester-Jose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
edit:
Problem solved.

I didn't realize that your could break up a variable declaration and assignment.

Sigh, this is why teachers who expect you to learn are annoying. Look it up they say. Or give vague hints.
[ September 09, 2005: Message edited by: Patrick Sylvester-Jose ]
 
Patrick Sylvester-Jose
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit:
Nevermind. I didn't realize that constructors had no return type.
[ September 09, 2005: Message edited by: Patrick Sylvester-Jose ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspected that might be the disconnect but didn't get into that kind of detail. Did you wind up with something like

This is nice because now FileIO is the only class that ever sees the File or Streams or BufferedReader objects. It hides them nicely from everyone else.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!