Character stream classes .52

330
-1

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
330
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Character stream classes .52

  1. 1. CharacterStream Classes http://improvejava.blogspot.in/ 1
  2. 2. ObjectivesOn completion of this period, you would be able to learn• CharacterStream classes http://improvejava.blogspot.in/ 2
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 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. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 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. 12. 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
  13. 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. 14. 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
  15. 15. 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
  16. 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. 17. Quiz1. Which stream is used to read an array of characters from a memory buffers 1. CharArrayReader 2. CharArrayWriter http://improvejava.blogspot.in/ 17
  18. 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. 19. Frequently Asked Questions• Write about CharArrayReader, CharArrayWriter• Write about BufferedReader, and BufferedWriter http://improvejava.blogspot.in/ 19

×