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