Low-Level And High Level Readers and Writers


Low-Level Reader And Writer. The Low-level readers and writers connect directly to a data source, similarly to memory or a file or socket.

High-level streams. High-level readers and writers connect to existing readers and writers.

Low Level Reader And Writer Example

CharArrayReader and CharArrayWriter. For reading from and writing to arrays of characters.

FileReader and FileWriter. For reading from and writing to files containing character data.

PipedReader and PipedWriter. For creating character streams between two threads.

StringReader and StringWriter. For reading from and writing to String objects.

High-Level Readers and Writers

BufferedReader and BufferedWriter. For buffering the characters in the character stream.

InputStreamReader and OutputStreamWriter. For converting between byte streams and character streams.

PrintWriter. For printing text to either an output stream or a Writer. You have seen a PrintWriter used extensively: System.out is a PrintWriter object.

PushbackReader. For readers that allow characters to be read and then pushed back into the stream.

Example

	import java.io.*;
	public class Input
	{
		public static void main(String [] args)
		{
		try
		{
		System.out.print("Enter your name:");
		InputStreamReader reader =
		new InputStreamReader(System.in);
		BufferedReader in =
		new BufferedReader(reader);
		String name = in.readLine();
		System.out.println("Hello, " + name
		+ ". Enter three ints...");
		int [] values = new int[3];
		double sum = 0.0;
		for(int i = 0; i < values.length; i++)
		{
		System.out.print("Number " + (i+1)
		 +" ");
		String temp = in.readLine();
		values[i] = Integer.parseInt(temp);
		sum += values[i];
		}
		System.out.println("The average equals "
		+ sum/values.length);
		}
		catch(IOException e)
		{
		e.printStackTrace();
		}
		}
}