Tuesday, May 10, 2011

Where do streams come from?

System.out is an OutputStream; specifically it's a PrintStream. There's a corresponding System.in which is an InputStream used to read data from the console.

Data for streams can also come from files. Later you'll see how to use the File class and the FileInputStream and FileOutputStream classes to read and write data from files.

Network connections commonly provide streams. When you connect to a web or ftp or some other kind of server, you read the data it sends from an InputStream connected from that server and write data onto an OutputStream connected to that server.

Java programs themselves produce streams. ByteArrayInputStreams, ByteArrayOutputStreams, StringBufferInputStreams, PipedInputStreams, and PipedOutputStreams all use the stream metaphor to move data from one part of a Java program to another.

Perhaps a little surprisingly, components like TextArea do not produce streams. However you can always use the strings they do produce to create a ByteArrayInputStream or a StringBufferInputStream.

No comments:

Post a Comment