Java Programming/Basic IO/Introduction to Streams
From Wikibooks, the open-content textbooks collection
[edit] Intro to Streams
A stream in Java is an ordered sequence of bytes of undetermined length. Streams are ordered and in sequence so that the java virtual machine can understand and work upon the stream. Streams are analogous to water streams.
Streams can be classed as both input and output streams. Input streams acquire bytes for our programmed java application/program and Output Streams direct streams of bytes outwards to the environment from our program or application.Also Java has streams to communicate across different parts of a program or even among threads.
The order or sequence of bytes in a Java stream allow the virtual machine to classify it among other streams. Streams exist as a communication medium, just like electromagnetic waves in communication. Java has various inbuilt streams implemented as classes. Some examples are: System.in and System.out. All Java streams are derived from Input Stream (java.io.InputStream) and Output Stream (java.io.OutputStream) classes. They are abstract base classes meant for other stream classes. The System.in is the input stream class derivative and analogically System.out is the output counterpart. Both are basic classes used to directly interact with input and output through console,similarly follows System.err.
[edit] Stream methods
Almost all input stream classes have these methods:
public abstract int read() throws IOException
public int read(byte[] data) throws IOException
public int read(byte[] data, int offset, int length) throws IOException
public long skip(long n) throws IOException
public int available() throws IOException
public void close() throws IOException public synchronized void mark(int readlimit)
public synchronized void reset() throws IOException
public boolean markSupported()
The first method returns byte as int, which can be casted into char.The second accepts an input sequence of ordered bytes into the specified byte array. The third method is analogous to the previous method, but uses an offset, which sets the start index of an array and length, that determines as to how many bytes to listen to and then copy into the augmented array. Fourth method skips 'n' future bytes and trashes it.Fifth method checks for availability of bytes. Eight and seventh methods don't occur in all streams and they help set flag/marks in bytes so that we can have custom seeks to those flagged locations.Eigth method resets and ninth method checks if marking/flagging is supported by the stream.The sixth method closes stream after proper flushing.
Almost all output stream classes have these methods:
public abstract void write(int b) throws IOException
public void write(byte[] data) throws IOException
public void write(byte[] data, int offset, int length) throws IOException
public void flush() throws IOException
public void close() throws IOException
[edit] Examples
import java.io.*; import java.net.*; /* * CLI program to download the content from a specified URL. * Usage: java WebDL http://site.com */ public class WebDL { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { download(args[i]); } } public static void download(String address) { OutputStream out = null; URLConnection conn = null; InputStream in = null; try { URL url = new URL(address); conn = url.openConnection(); String fileName = url.getHost() + ".txt"; out = new BufferedOutputStream( new FileOutputStream(fileName)); in = conn.getInputStream(); byte[] buffer = new byte[1024]; int iRead; while ((iRead = in.read(buffer)) != -1) { out.write(buffer, 0, iRead); } System.out.println("Contents written to " + fileName); } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException e) { ex.printStackTrace(); } } } }
Example: byte b=System.in.read();