Java- Character Streams

2.Character Streams

Character stream I/O automatically translates this internal format to and from the local character set. here the data is read by character by character

1. FileReader is meant for reading streams of characters

2. FileWriter is meant for writing streams of characters

Here Methods & Constructors are Similar to Byte Stream, but instead of byte they will char data. used for reading/writing data from/to Files by character encoding.

Example

public class CharacterStreams {
	public static void main(String[] args) throws IOException {
 String filepath = "E:\\users\\Kaveti_s\\Desktop\\Books\\tmp.txt";
 char[] ch ={ 'a', 'b', 'c', 'd', 'e' }; 
 FileWriter w = new FileWriter(filepath);
 	w.write(ch); //accepts char type only
 	w.close(); 

 FileReader r= new FileReader(filepath);
 int i;
 while ((i = r.read()) != -1) {
 	System.out.println(i+":"+(char)i);
 }
	}
}
-------------------
97:a	98:b	99:c	100:d	101:e

Here we can read file data. Data stored in the file is abcde

If we pass int data to FileWriter the program will execute without Compilation Error but it doesn’t display any Output / Empty Output