SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Wrapper class
Wrapper classWrapper class
Wrapper class
 
Applets
AppletsApplets
Applets
 
Files in java
Files in javaFiles in java
Files in java
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Interface
InterfaceInterface
Interface
 
Java-java virtual machine
Java-java virtual machineJava-java virtual machine
Java-java virtual machine
 
JAVA PROGRAMMING
JAVA PROGRAMMING JAVA PROGRAMMING
JAVA PROGRAMMING
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Java I/O
Java I/OJava I/O
Java I/O
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
 
Java threads
Java threadsJava threads
Java threads
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
 

Ähnlich wie Character stream classes introd .51

Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49myrajendra
 
Advanced programming ch2
Advanced programming ch2Advanced programming ch2
Advanced programming ch2Gera Paulos
 
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
 

Ähnlich wie Character stream classes introd .51 (20)

Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
 
Advanced programming ch2
Advanced programming ch2Advanced programming ch2
Advanced programming ch2
 
Input & output
Input & outputInput & output
Input & output
 
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
 

Mehr von 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