SlideShare uma empresa Scribd logo
1 de 16
CharacterStream classes




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

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




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

• In the previous class, we have studied about
  ByteArray input and output streams and Filtered
  Byte streams
• Data stored in memory is handled by
  • ByteArrayInput Stream class
  • ByteArrayOutputStream class
• Filtered Streams are used for converting bytes to
  useful forms such as char, string, int, etc


                  http://improvejava.blogspot.in/   3
Character Streams
• ByteStream classes handle the data in binary
  form which is efficient for data processing.
• They cannot work directly with Unicode
  characters
• CharacterStream deals with the character
  oriented text
• Characters are stored and retrieved in a human
  readable form



                http://improvejava.blogspot.in/    4
Character Streams                          contd..

• Character Streams have two abstract classes
  They are
  • Reader
  • Writer




                http://improvejava.blogspot.in/             5
Reader Class

• Is an abstract class that defines Java’s model of
  streaming character input
• All of the methods in this class will throw an
  IOException on error conditions




                  http://improvejava.blogspot.in/     6
Writer Class

• Is an abstract class that defines streaming
  character output
• All of the methods in this class returns a void
  value and
• throw an IOException in the case of errors




                 http://improvejava.blogspot.in/    7
FileReader Class

• FileReader class creates a Reader that you
  can use to read the contents of a file
• Two most commonly used constructors
  • FileReader(String filePath)
  • FileReader(File fileObj)




                  http://improvejava.blogspot.in/   8
Example
// Program to read a file and displays the same//
import java.io.*;
class FileReaderDemo {
public static void main(String args[]) throws
   Exception {
FileReader fr = new
   FileReader("FileReaderDemo.java");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
System.out.println(s);
}
fr.close(); } }
               http://improvejava.blogspot.in/   9
FileWriter class
• FileWriter creates a Writer that you can use to
  write to a file
• Its most commonly used constructors are
  •   FileWriter(String filePath)
  •   FileWriter(String filePath, boolean append)
  •   FileWriter(File fileObj)
  •   FileWriter(File fileObj, boolean append)
• They throw an IOException
• getChars( ) method extracts the character array
  equivalent


                     http://improvejava.blogspot.in/   10
Example:2
// Demonstrate FileWriter
import java.io.*;
class FileWriterDemo {
public static void main(String args[]) throws
   Exception {
String source = "Now is the time for all good
   menn"
+ " to come to the aid of their countryn"
+ " and pay their due taxes.";
char buffer[] = new char[source.length()];
source.getChars(0, source.length(), buffer, 0);
FileWriter f0 = new FileWriter("file1.txt");
               http://improvejava.blogspot.in/    11
Example:2                                        contd..

for (int i=0; i < buffer.length; i += 2) {
fo.write(buffer[i]); }
fo.close( );
FileWriter f1 = new FileWriter("file2.txt");
f1.write(buffer);
f1.close( );
FileWriter f2 = new FileWriter("file3.txt");
f2.write(buffer,buffer.length-
  buffer.length/4,buffer.length/4);
f2.close( ); } }
                http://improvejava.blogspot.in/             12
Summary

• Byte stream classes handle the data in binary
  form which is efficient for data processing
• Where as Character stream deals with the
  character oriented text




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

1. Character stream deals with the binary
   oriented data
  a) True
  b) False




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


2. Byte stream classes handle the data in
   character form
  a) True
  b) False




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

• Write about Character Stream classes
• Write about FileReader and FileWriter classes
• Write a java program which reads File form the
  hard disk and display the same onto monitor in
  character form




                http://improvejava.blogspot.in/   16

Mais conteúdo relacionado

Mais procurados (20)

Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Java applet - java
Java applet - javaJava applet - java
Java applet - java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
C++ oop
C++ oopC++ oop
C++ oop
 
Java I/O
Java I/OJava I/O
Java I/O
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Applets
AppletsApplets
Applets
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 

Semelhante a Character stream classes introd .51

Advanced programming ch2
Advanced programming ch2Advanced programming ch2
Advanced programming ch2Gera Paulos
 
L21 io streams
L21 io streamsL21 io streams
L21 io streamsteach4uin
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52myrajendra
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfVithalReddy3
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/ONem Sothea
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization Hitesh-Java
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, SerializationPawanMM
 
CHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxCHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxSadhilAggarwal
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.ioNilaNila16
 
Unit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptxUnit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptxDrYogeshDeshmukh1
 
Byte arrayinputstream.50
Byte arrayinputstream.50Byte arrayinputstream.50
Byte arrayinputstream.50myrajendra
 
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
 
Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
 
Java (1).ppt seminar topics engineering
Java (1).ppt  seminar topics engineeringJava (1).ppt  seminar topics engineering
Java (1).ppt seminar topics engineering4MU21CS023
 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introductionjyoti_lakhani
 

Semelhante a Character stream classes introd .51 (20)

Advanced programming ch2
Advanced programming ch2Advanced programming ch2
Advanced programming ch2
 
Input & output
Input & outputInput & output
Input & output
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/O
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
 
CHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxCHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptx
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
 
Unit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptxUnit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptx
 
Byte arrayinputstream.50
Byte arrayinputstream.50Byte arrayinputstream.50
Byte arrayinputstream.50
 
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
 
Java
JavaJava
Java
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Java (1).ppt seminar topics engineering
Java (1).ppt  seminar topics engineeringJava (1).ppt  seminar topics engineering
Java (1).ppt seminar topics engineering
 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introduction
 

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 introd .51

  • 1. CharacterStream classes http://improvejava.blogspot.in/ 1
  • 2. Objective On completion of this period, you would be able to learn • characterStream classes http://improvejava.blogspot.in/ 2
  • 3. Recap • In the previous class, we have studied about ByteArray input and output streams and Filtered Byte streams • Data stored in memory is handled by • ByteArrayInput Stream class • ByteArrayOutputStream class • Filtered Streams are used for converting bytes to useful forms such as char, string, int, etc http://improvejava.blogspot.in/ 3
  • 4. Character Streams • ByteStream classes handle the data in binary form which is efficient for data processing. • They cannot work directly with Unicode characters • CharacterStream deals with the character oriented text • Characters are stored and retrieved in a human readable form http://improvejava.blogspot.in/ 4
  • 5. Character Streams contd.. • Character Streams have two abstract classes They are • Reader • Writer http://improvejava.blogspot.in/ 5
  • 6. Reader Class • Is an abstract class that defines Java’s model of streaming character input • All of the methods in this class will throw an IOException on error conditions http://improvejava.blogspot.in/ 6
  • 7. Writer Class • Is an abstract class that defines streaming character output • All of the methods in this class returns a void value and • throw an IOException in the case of errors http://improvejava.blogspot.in/ 7
  • 8. FileReader Class • FileReader class creates a Reader that you can use to read the contents of a file • Two most commonly used constructors • FileReader(String filePath) • FileReader(File fileObj) http://improvejava.blogspot.in/ 8
  • 9. Example // Program to read a file and displays the same// import java.io.*; class FileReaderDemo { public static void main(String args[]) throws Exception { FileReader fr = new FileReader("FileReaderDemo.java"); BufferedReader br = new BufferedReader(fr); String s; while((s = br.readLine()) != null) { System.out.println(s); } fr.close(); } } http://improvejava.blogspot.in/ 9
  • 10. FileWriter class • FileWriter creates a Writer that you can use to write to a file • Its most commonly used constructors are • FileWriter(String filePath) • FileWriter(String filePath, boolean append) • FileWriter(File fileObj) • FileWriter(File fileObj, boolean append) • They throw an IOException • getChars( ) method extracts the character array equivalent http://improvejava.blogspot.in/ 10
  • 11. Example:2 // Demonstrate FileWriter import java.io.*; class FileWriterDemo { public static void main(String args[]) throws Exception { String source = "Now is the time for all good menn" + " to come to the aid of their countryn" + " and pay their due taxes."; char buffer[] = new char[source.length()]; source.getChars(0, source.length(), buffer, 0); FileWriter f0 = new FileWriter("file1.txt"); http://improvejava.blogspot.in/ 11
  • 12. Example:2 contd.. for (int i=0; i < buffer.length; i += 2) { fo.write(buffer[i]); } fo.close( ); FileWriter f1 = new FileWriter("file2.txt"); f1.write(buffer); f1.close( ); FileWriter f2 = new FileWriter("file3.txt"); f2.write(buffer,buffer.length- buffer.length/4,buffer.length/4); f2.close( ); } } http://improvejava.blogspot.in/ 12
  • 13. Summary • Byte stream classes handle the data in binary form which is efficient for data processing • Where as Character stream deals with the character oriented text http://improvejava.blogspot.in/ 13
  • 14. Quiz 1. Character stream deals with the binary oriented data a) True b) False http://improvejava.blogspot.in/ 14
  • 15. Quiz contd.. 2. Byte stream classes handle the data in character form a) True b) False http://improvejava.blogspot.in/ 15
  • 16. Frequently Asked Questions • Write about Character Stream classes • Write about FileReader and FileWriter classes • Write a java program which reads File form the hard disk and display the same onto monitor in character form http://improvejava.blogspot.in/ 16