The perfect place for easy learning...

Java Programming

×

Topics List


Character Stream in java





In java, when the IO stream manages 16-bit Unicode characters, it is called a character stream. The unicode set is basically a type of character set where each character corresponds to a specific numeric value within the given character set, and every programming language has a character set.

In java, the character stream is a 16 bits carrier. The character stream in java allows us to transmit 16 bits of data.

The character stream was introduced in Java 1.1 version. The charater stream

The java character stream is defined by two abstract classes, Reader and Writer. The Reader class used for character stream based input operations, and the Writer class used for charater stream based output operations.

The Reader and Writer classes have several concreate classes to perform various IO operations based on the character stream.

The following picture shows the classes used for character stream operations.

Character stream in java

Reader class

The Reader class has defined as an abstract class, and it has the following methods which have implemented by its concrete classes.

S.No. Method with Description
1 int read()

It reads the next character from the input stream.

2 int read(char[] cbuffer)

It reads a chunk of charaters from the input stream and store them in its byte array, cbuffer.

3 int read(char[] cbuf, int off, int len)

It reads charaters into a portion of an array.

4 int read(CharBuffer target)

It reads charaters into into the specified character buffer.

5 String readLine()

It reads a line of text. A line is considered to be terminated by any oneof a line feed ('\n'), a carriage return ('\r'), or a carriage returnfollowed immediately by a linefeed.

6 boolean ready()

It tells whether the stream is ready to be read.

7 void close()

It closes the input stream and also frees any resources connected with this input stream.

Writer class

The Writer class has defined as an abstract class, and it has the following methods which have implemented by its concrete classes.

S.No. Method with Description
1 void flush()

It flushes the output steam by forcing out buffered bytes to be written out.

2 void write(char[] cbuf)

It writes a whole array(cbuf) to the output stream.

3 void write(char[] cbuf, int off, int len)

It writes a portion of an array of characters.

4 void write(int c)

It writes single character.

5 void write(String str)

It writes a string.

6 void write(String str, int off, int len)

It writes a portion of a string.

7 Writer append(char c)

It appends the specified character to the writer.

8 Writer append(CharSequence csq)

It appends the specified character sequence to the writer

9 Writer append(CharSequence csq, int start, int end)

It appends a subsequence of the specified character sequence to the writer.

10 void close()

It closes the output stream and also frees any resources connected with this output stream.

Reading data using BufferedReader

We can use the BufferedReader class to read data from the console. The BufferedInputStream class needs InputStreamReaderclass. The BufferedReader use a method read( ) to read a value from the console, or file, or socket.

Let's look at an example code to illustrate reading data using BufferedReader.

Example 1 - Reading from console
import java.io.*;

public class ReadingDemo {

	public static void main(String[] args) throws IOException {

		 InputStreamReader isr = new InputStreamReader(System.in);    
	     BufferedReader in = new BufferedReader(isr);    
	     
	     String name = "";
	     
	     System.out.print("Please enter your name: ");
	     
	     name = in.readLine();
	     
	     System.out.println("Hello, " + name + "!");
	}
}

When we run the above program, it produce the following output.

Reader in java

Example 2 - Reading from a file
import java.io.*;

public class ReadingDemo {

	public static void main(String[] args) throws IOException {

		Reader in = new FileReader();
		
		try {
			char c = (char)input.read();
			System.out.println("Data read from a file - '" + c + "'");
		}
		catch(Exception e) {
			System.out.println(e);
		}
		finally {
			input.close();
		}
	}
}

When we run the above program, it produce the following output.

Reader in java

Writing data using FileWriter

We can use the FileWriter class to write data into the file. The FileWriter class use a method write( ) to write data.

Let's look at an example code to illustrate writing data into a file using FileWriter.

Example - Writing data into a file
import java.io.*;

public class WritingDemo {

	public static void main(String[] args) throws IOException {

		Writer out = new FileWriter("C:\\Raja\\dataFile.txt");
		
		String msg = "The sample data";
		
		try {
			out.write(msg);
			System.out.println("Writing done!!!");
		}
		catch(Exception e) {
			System.out.println(e);
		}
		finally {
			out.close();
		}
	}
}

When we run the above program, it produce the following output.

FileWriter in java