Character stream classes allow you to read and write character and string. The character are stored and retrieved form human readable Input.Character stream convert bytes to characters.
Character Stream is divided into two parts :
- Reader Stream
- Writer stream
Reader Stream
- Reader stream classes are used to read characters from file.
- The reader class provides a functionality that is available for all character input stream.
- The Reader class provides a following types of methods:
1. int read()
2. int read (char[] cbuffer)
3. int read(char[] cbuffer,int offset, int length)
Writer stream
- The Writer stream classes are used to perform all output operation on files
- The Writer class is an abstract which acts as base for all the other writer stream classes.
- The Writer class provides a following types of methods:
void write(int c) void write(char[] cbuffer) void write(char[] cbuffer,int offset, int length) void write(String s) void write(Sting s,int offset, int length)
Example:
package com.tech;
import java.io.*;
public class writerData
{
public writerData()
{ try
{
FileWriter fw =new FileWriter("D:\\WriteData1.txt");
String s ="This is the Example of Character Stream";
fw.write(s);
fw.flush();
fw.close();
System.out.println("successfully write");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
package com.tech;
import java.io.FileInputStream;
import java.io.FileReader;
public class readerData
{
public readerData()
{
try
{
FileReader fr =new FileReader("D:\\WriteData1.txt");
int i=0;
while((i=fr.read())!=-1)
{
System.out.print((char)i);
}
fr.close();
System.out.println("\nsuccessfully read");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
package com.tech;
public class Demo {
public static void main(String args[]) throws Exception
{
new writerData();
new readerData();
}
}
Output :
successfully write
This is the Example of Character Stream
successfully read
0 Comment(s)