CharacterStream Classes




       http://improvejava.blogspot.in/   1
Objectives

On completion of this period, you would be able
  to learn
• CharacterStream classes




               http://improvejava.blogspot.in/    2
Recap

In the last class, we have studied about
  CharacterStream classes
• Where character stream classes deal with the
  character oriented text




                http://improvejava.blogspot.in/   3
CharArrayReader class

• Is an implementation of an input stream that
  uses a character array as the source
• Is used to read an array of characters from a
  memory buffers
• Constructors
  • CharArrayReader(char array[ ])
  • CharArrayReader(char array[ ], int start, int
    numChars)



                   http://improvejava.blogspot.in/   4
Example:
// Demonstrate CharArrayReader.
import java.io.*;
public class CharArrayReaderDemo {
public static void main(String args[]) throws
   IOException {
string tmp = "abcdefghijklmnopqrstuvwxyz";
int length = tmp.length();
char c[] = new char[length];
tmp.getChars(0, length, c, 0);
CharArrayReader input1 =new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c,
                                                  0, 5);
                http://improvejava.blogspot.in/            5
Example:                                contd..

int i;
System.out.println("input1 is:");
while((i = input1.read()) != -1) {
System.out.print((char)i); }
System.out.println();
System.out.println("input2 is:");
while((i = input2.read()) != -1) {
System.out.print((char)i); }
System.out.println(); } }

           http://improvejava.blogspot.in/        6
CharArrayWriter class

• CharArrayWriter is an implementation of an
  output stream that uses an array as the
  destination
• It is used for writing an array of characters to a
  memory buffer
• CharArrayWriter has two constructors
   • CharArrayWriter( )
   • CharArrayWriter(int numChars)


                  http://improvejava.blogspot.in/      7
BufferedReader class
• BufferedReader improves performance by
  buffering input
• This class is used to increase the efficiency of
  reading
• It cannot directly access any source
• It has two constructors
   • BufferedReader(Reader input Stream)
   • BufferedReader(Reader input Stream, int bufSize)



                  http://improvejava.blogspot.in/       8
BufferedWriter class
• A Buffered Writer is a Writer that adds a
  flush( ) method that can be used
• To ensure that data buffers are physically
  written to the actual output stream
• Buffered Writer can increase performance by
  reducing the number of times data is actually
  physically written to the output stream
• Constructors
  • BufferedWriter(Writer outputStream)
  • BufferedWriter(Writer outputStream, int bufSize)

                 http://improvejava.blogspot.in/       9
Example:2


// Program for word count for a given input file
import java.io.*;
class WordCount {
public static int words = 0;
public static int lines = 0;
public static int chars = 0;



               http://improvejava.blogspot.in/   10
Example:2                                      contd..

public static void wc (InputStreamReader isr)
throws IOException {
int c = 0;
boolean lastWhite = true;
String whiteSpace = " tnr";
while ((c = isr.read()) != -1) {




             http://improvejava.blogspot.in/             11
Example:2                                       contd..

// count characters
chars++;
if (c == 'n') {
// Count lines
lines++; }
// Count words by detecting the start of a word
int index = whiteSpace.indexOf(c);
}


              http://improvejava.blogspot.in/             12
Example:2                                      contd..

   if(index == -1) {
   if(lastWhite == true) {
   ++words; }
   lastWhite = false; }
   else {
   lastWhite = true; }     }
   if(chars != 0) {
   ++lines; } }

             http://improvejava.blogspot.in/             13
Example:2                           contd..
public static void main(String args[]) {
FileReader fr;
try {
if (args.length == 0) { // We're working with stdin
wc(new InputStreamReader(System.in)); }
else { // We're working with a list of files
for (int i = 0; i < args.length; i++) {
fr = new FileReader(args[i]);
wc(fr);            }     }     }
catch (IOException e) {
return; }
System.out.println(lines + " " + words + " " +
   chars); } }
                http://improvejava.blogspot.in/   14
Summary

• CharacterArrayReader an implementation of an
  input stream that uses a character array as the
  source and
• Used to read an array of characters from a
  memory buffers
• CharArrayWriter is used for writing an array of
  characters to a memory buffer




                 http://improvejava.blogspot.in/    15
Summary                       contd..

• BufferedReader improves performance by
  buffering input
• BufferWriter ensures that data buffers are
  physically written to the actual output stream




                  http://improvejava.blogspot.in/   16
Quiz

1. Which stream is used to read an array of
    characters from a memory buffers
   1. CharArrayReader
   2. CharArrayWriter




              http://improvejava.blogspot.in/   17
Quiz                           contd..

2.    Which stream is used for writing an array of
      characters to a memory buffer
     1. CharArrayWriter
     2. CharArrayReader




                   http://improvejava.blogspot.in/             18
Frequently Asked Questions

• Write about CharArrayReader, CharArrayWriter
• Write about BufferedReader, and BufferedWriter




                  http://improvejava.blogspot.in/   19

Character stream classes .52

  • 1.
    CharacterStream Classes http://improvejava.blogspot.in/ 1
  • 2.
    Objectives On completion ofthis period, you would be able to learn • CharacterStream classes http://improvejava.blogspot.in/ 2
  • 3.
    Recap In the lastclass, we have studied about CharacterStream classes • Where character stream classes deal with the character oriented text http://improvejava.blogspot.in/ 3
  • 4.
    CharArrayReader class • Isan implementation of an input stream that uses a character array as the source • Is used to read an array of characters from a memory buffers • Constructors • CharArrayReader(char array[ ]) • CharArrayReader(char array[ ], int start, int numChars) http://improvejava.blogspot.in/ 4
  • 5.
    Example: // Demonstrate CharArrayReader. importjava.io.*; public class CharArrayReaderDemo { public static void main(String args[]) throws IOException { string tmp = "abcdefghijklmnopqrstuvwxyz"; int length = tmp.length(); char c[] = new char[length]; tmp.getChars(0, length, c, 0); CharArrayReader input1 =new CharArrayReader(c); CharArrayReader input2 = new CharArrayReader(c, 0, 5); http://improvejava.blogspot.in/ 5
  • 6.
    Example: contd.. int i; System.out.println("input1 is:"); while((i = input1.read()) != -1) { System.out.print((char)i); } System.out.println(); System.out.println("input2 is:"); while((i = input2.read()) != -1) { System.out.print((char)i); } System.out.println(); } } http://improvejava.blogspot.in/ 6
  • 7.
    CharArrayWriter class • CharArrayWriteris an implementation of an output stream that uses an array as the destination • It is used for writing an array of characters to a memory buffer • CharArrayWriter has two constructors • CharArrayWriter( ) • CharArrayWriter(int numChars) http://improvejava.blogspot.in/ 7
  • 8.
    BufferedReader class • BufferedReaderimproves performance by buffering input • This class is used to increase the efficiency of reading • It cannot directly access any source • It has two constructors • BufferedReader(Reader input Stream) • BufferedReader(Reader input Stream, int bufSize) http://improvejava.blogspot.in/ 8
  • 9.
    BufferedWriter class • ABuffered Writer is a Writer that adds a flush( ) method that can be used • To ensure that data buffers are physically written to the actual output stream • Buffered Writer can increase performance by reducing the number of times data is actually physically written to the output stream • Constructors • BufferedWriter(Writer outputStream) • BufferedWriter(Writer outputStream, int bufSize) http://improvejava.blogspot.in/ 9
  • 10.
    Example:2 // Program forword count for a given input file import java.io.*; class WordCount { public static int words = 0; public static int lines = 0; public static int chars = 0; http://improvejava.blogspot.in/ 10
  • 11.
    Example:2 contd.. public static void wc (InputStreamReader isr) throws IOException { int c = 0; boolean lastWhite = true; String whiteSpace = " tnr"; while ((c = isr.read()) != -1) { http://improvejava.blogspot.in/ 11
  • 12.
    Example:2 contd.. // count characters chars++; if (c == 'n') { // Count lines lines++; } // Count words by detecting the start of a word int index = whiteSpace.indexOf(c); } http://improvejava.blogspot.in/ 12
  • 13.
    Example:2 contd.. if(index == -1) { if(lastWhite == true) { ++words; } lastWhite = false; } else { lastWhite = true; } } if(chars != 0) { ++lines; } } http://improvejava.blogspot.in/ 13
  • 14.
    Example:2 contd.. public static void main(String args[]) { FileReader fr; try { if (args.length == 0) { // We're working with stdin wc(new InputStreamReader(System.in)); } else { // We're working with a list of files for (int i = 0; i < args.length; i++) { fr = new FileReader(args[i]); wc(fr); } } } catch (IOException e) { return; } System.out.println(lines + " " + words + " " + chars); } } http://improvejava.blogspot.in/ 14
  • 15.
    Summary • CharacterArrayReader animplementation of an input stream that uses a character array as the source and • Used to read an array of characters from a memory buffers • CharArrayWriter is used for writing an array of characters to a memory buffer http://improvejava.blogspot.in/ 15
  • 16.
    Summary contd.. • BufferedReader improves performance by buffering input • BufferWriter ensures that data buffers are physically written to the actual output stream http://improvejava.blogspot.in/ 16
  • 17.
    Quiz 1. Which streamis used to read an array of characters from a memory buffers 1. CharArrayReader 2. CharArrayWriter http://improvejava.blogspot.in/ 17
  • 18.
    Quiz contd.. 2. Which stream is used for writing an array of characters to a memory buffer 1. CharArrayWriter 2. CharArrayReader http://improvejava.blogspot.in/ 18
  • 19.
    Frequently Asked Questions •Write about CharArrayReader, CharArrayWriter • Write about BufferedReader, and BufferedWriter http://improvejava.blogspot.in/ 19