• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Stream » Character Stream Classes in Java
Next →
← Prev

Character Stream Classes in Java

By Dinesh Thakur

One of the limitations of byte stream classes is that it can handle only 8-bit bytes and cannot work directly with Unicode characters. To overcome this limitation, character stream classes have been introduced in java.io package to match the byte stream classes. The character stream classes support 16-bit Unicode characters, performing operations on characters, character arrays, or strings, reading or writing buffer at a time. Character stream classes are divided into two stream classes namely, Reader class and Writer class.

We’ll be covering the following topics in this tutorial:

  • Reader Classes
  • Writer Classes

Reader Classes

Reader classes are used to read 16-bit unicode characters from the input stream. The Reader class is the superclass for all character-oriented input stream classes. All the methods of this class throw an IOException. Being an abstract class, the Reader class cannot be instantiated hence its subclasses are used. Some of these are listed in Table

                                                                Table Reader Classes

Class

Description

BufferedReader

contains methods to read characters from the buffer

CharArrayReader

contains methods to read characters from a character array

FileReader

contains methods to read from a file

FilterReader

contains methods to read from underlying character-input stream

InputStreamReader

contains methods to convert bytes to characters

PipedReader

contains methods to read from the connected piped output stream

StringReader

contains methods to read from a string

The Reader class defines various methods to perform reading operations on data of an input stream. Some of these methods along with their description are listed in Table.

                                                                Table Reader Class Methods

Method

Description

int read()

returns the integral representation of the next available character of input. It returns -1 when end of file is encountered

int read (char buffer [])

attempts to read buffer. length characters into the buffer and returns the total number of characters successfully read. It returns -I when end of file is encountered

int read (char buffer [], int loc, int nChars)

attempts to read ‘nChars’ characters into the buffer starting at buffer [loc] and returns the total number of characters successfully read. It returns -1 when end of file is encountered

void mark(int nChars)

marks the current position in the input stream until ‘nChars’ characters are read

void reset ()

resets the input pointer to the previously set mark

long skip (long nChars)

skips ‘nChars’ characters of the input stream and returns the number of actually skipped characters

boolean ready ()

returns true if the next request of the input will not have to wait, else it returns false

void close ()

closes the input source. If an attempt is made to read even after closing the stream then it generates IOException

Using Buffered.Reader Class

The BufferedReader class creates a buffered character stream between the input device and the program. It defines the methods to read the data from the buffer in the form of characters.

The BufferedReader object can be created using one of the following two constructors.

BufferedReader(Reader inpStream) //first

BufferedReader(Reader inpStream, int nChars) //second

The first constructor creates a buffered character stream of default buffer size. The second constructor creates a buffered character stream that uses an input buffer of size nChars.

Besides the methods provided by the Reader class, the BufferedReader class contains some other methods which are discussed as follows.

• String readLine ():It reads a line of the input text.

• boolean ready () : It checks whether or not the stream is ready to be read.

A program to demonstrate the use of BufferedReader class

import java.io.*;
class BufferedReaderExample {
       publicstaticvoid main(String args[])throwsIOException{
             String s=“This program is skipping first character of each word in the string”;
             StringReader sr = newStringReader(s);
             System.out.println(“The input string is: “+s);
             /*Creating an instance of BufferedReader using StringReader*/
             BufferedReader br = newBufferedReader(sr);
             //Reading from the underlying StringReader
             System.out.print (“The output string is: “);
             br.skip(1);
             /*skipping the first character of the first word of the input string*/
             //reading the remaining string
             int i=0;
             while((i=br.read())!=–1){
                   System.out.print((char)i);
                   if((char)i==‘ ‘)
                       //checking for white spaces
                       {
                               br.skip(1);
                               /*skipping first character of each word*/
                        }
             }
     }
}

The output of the program is

The input string is: This program is skipping first character of each word in the string

The output string is: his rogram s kipping irst haracter f ach ord n he tring In this program, skip () method is used to skip the first character of each word of the input string.

Writer Classes

Writer classes are used to write 16-bit Unicode characters onto an outputstream. The Writer class is the superclass for all character-oriented output stream classes .All the methods of this class throw an IOException. Being an abstract class, the Writer class cannot be instantiated hence, its subclasses are used. Some of these are listed in Table.

                                                            Table Writer Classes

Class

Description

BufferedWriter

Contains methods to write characters to a buffer

FileWriter

Contains methods to write to a file

FilterWriter

Contains methods to write characters to underlying output stream

CharArrayWriter

Contains methods to write characters to a character array

OutputStreamWriter

Contains methods to convert from bytes to character

PipedWriter

Contains methods to write to the connected piped input stream

StringWriter

Contains methods to write to a string

The Writer class defines various methods to perform writing operations on output stream. These methods along with their description are listed in Table.

                                                      Table Writer Class Methods

Method

Description

void write ()

writes data to the output stream

void write (int i)

Writes a single character to the output stream

void write (char buffer [] )

writes an array of characters to the output stream

void write(char buffer [],int loc, int nChars)

writes ‘n’ characters from the buffer starting at buffer [loc] to the output stream

void close ()

closes the output stream. If an attempt is made to perform writing operation even after closing the stream then it generates IOException

void flush ()

flushes the output stream and writes the waiting buffered output characters

Using BufferedWriter Class

The BufferedWriter class, an output counterpart of the BufferedReader, is used to write characters onto the character-output stream.

The BufferedWriter object can be created using one of the following two constructors.

BufferedWriter(Writer outStream) //first

BufferedWriter(Writer outStream, int nChars) //second

The first constructor creates a buffered character stream of default buffer size. The second constructor creates a buffered character stream that uses an input buffer of size nChars. Besides the methods provided by the Writer class, the BufferedWriter class contains the newLine () method which writes a new line.

//A program to demonstrate the use of BufferedWriter class

The output of the program is

This is an example of BufferedWriter

import java.io.*;
class BufferedWriterExample {
        public static void main(String args[]) throws IOException {
               String s = “This is an example of BufferedWriter”;
               StringWriter sw = new StringWriter();
               //creating an instance of BufferedWriter class
               BufferedWriter bw = new BufferedWriter(sw);
               bw.write(s,0,5);
               bw.newLine();
               bw.write(s,5,s.length()–5);
               /*writes the substring starting from index 5*/
               bw.flush() ;
              //flushes the stream
               System.out.println(sw.getBuffer());
               sw.close();
               //closing the stream
               bw.close();
               //closing the stream
       }
}

In this example the BufferedWriter class is used to write data to character-output stream (instance of StringWriter class). The getBuffer () method of the StringWriter class returns a buffer of String type.

You’ll also like:

  1. Byte Stream Classes in java
  2. File input Stream and File Output Stream
  3. C Program Replace Character with an Equivalent Encoded Character
  4. Copying Contents after Converting Each Character in Capital in a File Java Example
  5. Find out the ASCII value from character in Java Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW