Example Of FileWriter And FileReader

package fileionavigationexample;
import java.io.*;
public class FileWriterAndFileReader {
public static void main(String[] args){
char[] in = new char[100];
int size = 0;
try{
File file = new File(“C:\Users\KUNDAN\Documents\Firsttext.txt”);
FileWriter fw = new FileWriter(file);//this is a file writer object “fw”.
fw.write(“Hello I am writing a story.”);//it is used to write character into the text file “Firsttext.txt”.
// fw.write(“Hello I am writing a story.”);
fw.close();

FileReader fr = new FileReader(file);
size = fr.read(in);//it reads the total number of bytes in a file.
for(char c:in){
System.out.print(c);
}
System.out.println(size); //return 26(21 letters + 5 spaces b/w letters)
fr.close();

}catch(IOException ex){
ex.printStackTrace();
}
}
}

/*
Output:
————-
Hello I am writing a story 26 bytes
*/

Leave a Reply