Sunday, June 15, 2014

What are FileReader and FileWriter ? here are examples

FileReader and FileWriter read and write charactors not bytes

its for text

new file
new file writer
write rite rite

flush (ensures on disk not in memory)
close

reader can asynch read files
writer creates file if not in existence

an example
Usage of FileWriter can be explained as follows :
File file = new File("fileWrite2.txt");
FileWriter fw = new FileWriter(file);
for(int i=0;i<10;i++){
fw.write("Soham is Just Awesome : "+i);
fw.flush();
}
fw.close();
Usage of FileWriter and FileReader used in conjunction is as follows:
int c;
FileReader fread = new FileReader("xanadu.txt");
FileWriter fwrite = new FileWriter("characteroutput.txt");
while ((c = fread.read()) != -1)
       fwrite.write(c);

No comments:

Post a Comment