Understanding Various I/O systems java

115 Views Asked by At

i have been reading through the java tutorials on oracle.com recently and i am trying to understand I/O. But there are so many things, a lot of which appear to be the same, such as: Data streams Scanner printwriter buffered streams file i/o streams filtered streams etc.

specifically, for example, what is the difference between a scanner and a data stream?

at first i could keep on top of it all but there were just too many streams and i/os. does anyone know of a good tutorial or website that could get all of this clear. my lack of understanding is starting to get annoying.

2

There are 2 best solutions below

0
On

A Scanner can be attached to a File, Stream, or String, Readable, or anything that derives from those classes. Think of it like a consumer. It eats things when you ask it to.

A Stream is a source. Some input streams have a signal that says "I'm out of input!" If you continue to consume a stream that is already out of input, Java usually waits for more input or simply throws an exception. It is important to note that once a stream is consumed, it is gone forever. Some readers have mechanisms that will "reset" a stream to an earlier point, but these fancy schmancy mechanisms are not part of a standard stream. Think of them like rivers. Once you let part of the river flow by, it's gone.

System.in is one example of an InputStream that is, by default, hooked to the console. Imagine it is feeding straight from your keyboard. You can attach a Scanner to it and attempt to consume input from the user. Scanner can capture things that you type, but if you don't store it in a variable, your keyboard isn't going to type itself again.

0
On

This is a tough question to answer. I know previous when I was learning network programming I had the same confusion.
You could take a look at Lars Vogel's tutorials - he writes good code that make things easy to understand: http://www.vogella.com/tutorials/JavaIO/article.html

Also if you have a good book, the Java classes may make more sense after you get familiar with them. Elliot "Rusty" Harold makes a good Java Network programming book: Java Network Programming

You need to get familiar with the "java.io.*" classes and what each one might be used for. There is a reason why there are so many, since you would use one particular class for one type of application.