DEV Community

Divya Divya
Divya Divya

Posted on • Edited on

File Handling Classes

File Class:

The File class in Java is used to represent and manage files and directories. It provides methods to create files, check whether a file exists, get file information such as name and path, and delete files. It belongs to the java.io package.

The File class does not read or write data; it is mainly used for file and directory management.

Example:

import java.io.File;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {

        File file = new File("sample.txt");

        file.createNewFile();

        System.out.println("File Created");
    }
}
Enter fullscreen mode Exit fullscreen mode

FileWriter:

  • FileWriter is a class in Java used to write character or text data into a file.
  • It belongs to the java.io package.
  • It provides methods such as write() to write data , flush() method is used to Save the written text immediately. close() to close the file after writing. The close() method automatically performs a flush before closing.
  • FileWriter is used to store data permanently in text files.

Example

import java.io.FileWriter;

public class Main {
    public static void main(String[] args) throws Exception {

        FileWriter fw = new FileWriter("test.txt", true);

        fw.write("Hello ");
        fw.flush();   // Data immediately written to file

        fw.write("World");
        fw.close();   // Writes remaining data and closes file
    }
}
Enter fullscreen mode Exit fullscreen mode

true => Append mode. Existing data is not deleted; new data is added at the end.

FileReader:

FileReader is a class in Java used to read character or text data from a file. It belongs to the java.io package and provides the

  • read() method to read data from files.
  • read() reads one character at a time.
  • Each character is returned the ASCII value.
  • (char) ch converts the integer back to a character.
  • When no more characters are left, read() returns -1.
  • The loop stops and close() closes the file.

Example

FileReader fr = new FileReader("test.txt");

int ch;
while ((ch = fr.read()) != -1) {
    System.out.print((char) ch);
}

fr.close();
Enter fullscreen mode Exit fullscreen mode

BuffeReader:

BufferedReader is a Java class used to read text data from a file or input stream efficiently. It reads data line by line using a buffer, making reading faster.
It provides the readLine() method, which reads one line at a time and returns it as a String. If there is no more data, it returns null.

Example

BufferedReader br = new BufferedReader(new FileReader("test.txt"));

String line = br.readLine();

System.out.println(line);

br.close();
Enter fullscreen mode Exit fullscreen mode

BuferWriter:

BufferedWriter is a Java class used to write text data to a file efficiently using a buffer.
It reduces the number of file write operations and provides methods such as write(),newLine(), flush(), and close().

Example:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedWriter bw = new BufferedWriter(
                                new FileWriter("sample.txt"));

        bw.write("Ram");
        bw.newLine();      // Move to next line
        bw.write("Priya");

        bw.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)