This week, we're going to demonstrate some of the ways in which streams are used. Using Streams in Creating Java Applets
Our first example is very simple--it reads from a stream (one line at a time) and prints the output to Java's standard output stream (you'll see it in your Java console). There are a few twists, however. The average applet does not have any chance to open files, which are probably the most common source of streams, and we thought using the network here would make the example too complicated, so we used one of the tools we mentioned last week instead. Namely, we turned a String into a stream we could read.
But it wouldn't be nearly as interesting an example if we didn't let you control what went into that String. The applet we've written is triggered by a mouse click; when you click on the text box, its contents (represented as a String) are read with a
StringBufferInputStream. If you open your Java console (Netscape users will find this in the options menu), you can see the applet's output.
Take a look at the source file for the applet. It required us to mix in a few things we've learned in weeks past--a little bit of user interface and event handling--but the important part should be indicated by the comments. We create a
StringBufferInputStreamusing the string we get from theTextArea, and then we use that to create aDataInputStream, so we can read the stream line by line (mystring = dis.readLine()).Notice that we read the entire stream by simply continuing to call
readLine()until it returned a null instead of a String. This is a typical way to tell when you've hit the end of a stream. Of course, should you attempt to read past the end, or perpetrate some other shenanigans, you'd end up with an I/O Exception.We could have eschewed
DataInputStreamand itsreadLineentirely, had we wanted to. There are several alternatives, the easiest being to simply use thereadandwritemethods ourselves to read and write a byte at a time. This is pretty straightforward, and it's exactly what we do in our second example:
Once again, here's the source to examine. We changed a few things with this second applet, most notably using a different stream--the
LineNumberInputStream. Now instead of attaching the String's stream to aDataInputStream, we attach it to this line numbering stream, which does nothing much except to keep track of the line number we're on while we read from it.You can see the results by looking in your Java console after you've typed a few lines in the box. The line numbers appear at the beginning of every line. Not automatically, of course, but because we have them available, we can print them every time we read a new line ourselves.
You may be wondering (especially if you haven't looked at the
LineNumberInputStreamdescription yet) why we didn't keep the slightly easier method of reading and printing the stream line by line, like we did in the first example. We did it this way to illustrate how it may be done; in fact, we might have created aDataInputStreamusing ourLineNumberInputStreamand used them both, in a manner of speaking, although this involves a few other minor issues we can't get into here.Let's sum up by looking at the common features of these two applets. Both took their input from a String, but that's really unimportant; these I/O classes behave the same regardless of what kind of input stream drives them. Both included a
tryandcatchsection, since reading from or writing to any kind of stream is generally error prone. Both only copied the contents of one stream to another, but that's the least of what you can do. Next week we'll finish off our coverage of streams with some more complicated examples to back that up.