- File Class :
- The file class is represents file or directory(folder).They used to file related operations such as creating,deleting,renaming,check whether file or folder.
- However the file does not write or read.
Example:
File file = new File("data.txt");
if(file.exists()) {
System.out.println("File exists");
}
2.FileReader:
The FileReader is used to read text data from a file by character by character.
It is suitable for reading small text files.
Example:
FileReader fr = new FileReader("data.txt");
int ch;
while((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
fr.close();
- FileWriter :
- The FileWriter is used to write text data into a file . If the file does not exist ,java creates file automatically.
Example:
FileWriter fw = new FileWriter("data.txt");
fw.write("Hello Java");
fw.close();
- BufferedReader :
- The BufferReader is used to improves reading performance by instead of using internal buffer.
- Instead of accessing the file for every character, it reads a larger block of data into memory. It also provides the convenient readLine() method.
Example:
BufferedReader br =
new BufferedReader(
new FileReader("data.txt"));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
5.BufferedWriter :
-The BufferedWriter is used to write text data improves writing performance by storing data in a buffer before writing it to the file.
- The reduces the number of disk operatins and makes file writing faster.
Example:
BufferedWriter bw =
new BufferedWriter(
new FileWriter("data.txt"));
bw.write("Hello");
bw.newLine();
bw.write("Java");
bw.close();
Top comments (0)