• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Character stream classes .52
 

Character stream classes .52

on

  • 397 views

 

Statistics

Views

Total Views
397
Views on SlideShare
347
Embed Views
50

Actions

Likes
0
Downloads
7
Comments
0

3 Embeds 50

http://www.ustudy.in 38
http://ustudy.in 7
http://improvejava.blogspot.in 5

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Character stream classes .52 Character stream classes .52 Presentation Transcript

    • CharacterStream Classes http://improvejava.blogspot.in/ 1
    • ObjectivesOn completion of this period, you would be able to learn• CharacterStream classes http://improvejava.blogspot.in/ 2
    • RecapIn 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 fileimport 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 characterschars++;if (c == n) {// Count lineslines++; }// Count words by detecting the start of a wordint 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) { // Were working with stdinwc(new InputStreamReader(System.in)); }else { // Were working with a list of filesfor (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
    • Quiz1. 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