• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Character stream classes introd  .51
 

Character stream classes introd .51

on

  • 463 views

 

Statistics

Views

Total Views
463
Views on SlideShare
444
Embed Views
19

Actions

Likes
0
Downloads
2
Comments
0

1 Embed 19

http://improvejava.blogspot.in 19

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Character stream classes introd  .51 Character stream classes introd .51 Presentation Transcript

    • CharacterStream classes http://improvejava.blogspot.in/ 1
    • ObjectiveOn 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 FileWriterimport 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
    • Quiz1. 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