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.
A
Scanner
can be attached to aFile
,Stream
, orString
,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 anInputStream
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.