J2EEOnline J2EE 

Java Streams  «Prev  Next»
Lesson 6 The stream classes
ObjectiveFamiliarize yourself with the stream classes in the java.io package.
Most classes that work directly with streams are part of the java.io package. There are also a few streams classes in java.util.zip.
The java.util.zip package contains four input stream classes that read data in a compressed format and return it in uncompressed format and four output stream classes that read data in uncompressed format and write in compressed format.
The two main classes are
  1. java.io.InputStream and
  2. java.io.OutputStream

These are abstract base classes for many different subclasses with more-specialized abilities.
Although InputStream and OutputStream are abstract classes, many methods in the class library are only specified to return an InputStream or OutputStream, not the more-specific subclass.
The ByteArrayOutputStream class writes data into the successive components of a byte array using the methods of java.io.OutputStream :
public class ByteArrayOutputStream extends OutputStream


You can read the contents of a web page with this sequence of commands.
String address = "http://java.sun.com/index.html";
URL u = new URL(address);
URLConnection connection = u.openConnection();
InputStream stream = connection.getInputStream();
Scanner in = new Scanner(stream);
Some of these methods may throw exceptions

Stream Basics Quiz
Click the Quiz link below to take a brief multiple-choice quiz on the basics of streams.
Stream Basics Quiz