SlideShare uma empresa Scribd logo
1 de 19
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

Mais conteúdo relacionado

Mais procurados

Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streamsbabak danyal
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)Om Ganesh
 
IO and serialization
IO and serializationIO and serialization
IO and serializationbackdoor
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in javasharma230399
 
IO In Java
IO In JavaIO In Java
IO In Javaparag
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in javaJayasankarPR2
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and filesMarcello Thiry
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentationAzeemaj101
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Filesprimeteacher32
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
14 file handling
14 file handling14 file handling
14 file handlingAPU
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output ConceptsVicter Paul
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Outputphanleson
 
Using Input Output
Using Input OutputUsing Input Output
Using Input Outputraksharao
 

Mais procurados (20)

Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
IO and serialization
IO and serializationIO and serialization
IO and serialization
 
Java stream
Java streamJava stream
Java stream
 
32.java input-output
32.java input-output32.java input-output
32.java input-output
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
 
IO In Java
IO In JavaIO In Java
IO In Java
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentation
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
 
Java stereams
Java stereamsJava stereams
Java stereams
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
14 file handling
14 file handling14 file handling
14 file handling
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
 
Using Input Output
Using Input OutputUsing Input Output
Using Input Output
 
Java I/O
Java I/OJava I/O
Java I/O
 

Destaque

Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28myrajendra
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in javaRicha Singh
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaAdil Mehmoood
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object ReferencesTareq Hasan
 
Collections Framework
Collections FrameworkCollections Framework
Collections FrameworkSunil OS
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and OperatorsSunil OS
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan Samoon
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/ServletSunil OS
 
Java Basics
Java BasicsJava Basics
Java BasicsSunil OS
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 

Destaque (18)

Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Inner classes
Inner classesInner classes
Inner classes
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in java
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 

Semelhante a Character stream classes .52

Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6Berk Soysal
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scannerArif Ullah
 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptxssuser9d7049
 
Class notes(week 5) on command line arguments
Class notes(week 5) on command line argumentsClass notes(week 5) on command line arguments
Class notes(week 5) on command line argumentsKuntal Bhowmick
 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptxRathanMB
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptxcherryreddygannu
 
Class notes(week 5) on command line arguments
Class notes(week 5) on command line argumentsClass notes(week 5) on command line arguments
Class notes(week 5) on command line argumentsKuntal Bhowmick
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.ioNilaNila16
 
Java OOP Concepts 1st Slide
Java OOP Concepts 1st SlideJava OOP Concepts 1st Slide
Java OOP Concepts 1st Slidesunny khan
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesSakkaravarthiS1
 
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals WebStackAcademy
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and outputRiccardo Cardin
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsDon Bosco BSIT
 
JAVASCRIPT PPT [Autosaved].pptx
JAVASCRIPT PPT [Autosaved].pptxJAVASCRIPT PPT [Autosaved].pptx
JAVASCRIPT PPT [Autosaved].pptxAchieversITAravind
 
Full Stack Online Course in Marathahalli| AchieversIT
Full Stack Online Course in Marathahalli| AchieversITFull Stack Online Course in Marathahalli| AchieversIT
Full Stack Online Course in Marathahalli| AchieversITAchieversITAravind
 

Semelhante a Character stream classes .52 (20)

Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scanner
 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Class notes(week 5) on command line arguments
Class notes(week 5) on command line argumentsClass notes(week 5) on command line arguments
Class notes(week 5) on command line arguments
 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptx
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
Class notes(week 5) on command line arguments
Class notes(week 5) on command line argumentsClass notes(week 5) on command line arguments
Class notes(week 5) on command line arguments
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
 
Java OOP Concepts 1st Slide
Java OOP Concepts 1st SlideJava OOP Concepts 1st Slide
Java OOP Concepts 1st Slide
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf NotesUNIT4-IO,Generics,String Handling.pdf Notes
UNIT4-IO,Generics,String Handling.pdf Notes
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and output
 
5java Io
5java Io5java Io
5java Io
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
 
JAVASCRIPT PPT [Autosaved].pptx
JAVASCRIPT PPT [Autosaved].pptxJAVASCRIPT PPT [Autosaved].pptx
JAVASCRIPT PPT [Autosaved].pptx
 
Full Stack Online Course in Marathahalli| AchieversIT
Full Stack Online Course in Marathahalli| AchieversITFull Stack Online Course in Marathahalli| AchieversIT
Full Stack Online Course in Marathahalli| AchieversIT
 

Mais de myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Character stream classes .52

  • 1. CharacterStream Classes http://improvejava.blogspot.in/ 1
  • 2. Objectives On completion of this period, you would be able to learn • CharacterStream classes http://improvejava.blogspot.in/ 2
  • 3. 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
  • 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. 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. 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 • 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. 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. 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. 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
  • 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 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. 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 stream is 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