Files in Java

In java file is class which is available in java.io package is used to perform various operations on files and directories.

How to create a file object in java
  
    File aFile = new File("path");
  
Most useful methods from Files
Operation Method Name Package
To create file createNewFile() java.io.File
To read file read() java.io.FileReader
To write file write() java.io.FileWriter
To delete file delete() java.io.File
Create a new file in Java

File class is used to create the file at specified position, in order to work with files in java first we need to create the file. While creation of file we need to check the file is already available or not if file is not available then only we need to create the file. Below example will talk about only creation of the file.


  import java.io.File;

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

      // create a file object for the current location
      File file = new File("newFile.txt");

      try {

        // trying to create a file based on the object
        boolean value = file.createNewFile();
        if (value) {
          System.out.println("The new file is created.");
        }
        else {
          System.out.println("The file already exists.");
        }
      }
      catch(Exception e) {
        e.getStackTrace();
      }
    }
  }

FileWriter in Java

The FileWriter is a class which is available in java.io package. Which is used to write data to the file.

Create a FileWriter

In order to create a FileWriter, we must import the java.io.FileWriter package. Once we import the package, as shown below we can create the FileWriter class object. Since Java 11 we can specify the type of character encoding (UTF-8 or UTF-16) in the file as well

  
    FileWriter output = new FileWriter(String name); // here name is path
    FileWriter output = new FileWriter(File aFile); // here aFile is an object
    FileWriter input = new FileWriter(String file, Charset cs); // file path and character encoding
  
Wrtie data to the file

  // importing the FileWriter class
  import java.io.FileWriter;

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

      String data = "This is the data in the output file";
      try {
        // Creates a Writer using FileWriter
        FileWriter output = new FileWriter("output.txt");

        // Writes string to the file
        output.write(data);
        System.out.println("Data is written to the file.");

        // Closes the writer
        output.close();
      }
      catch (Exception e) {
        e.getStackTrace();
      }
    }
  }

BufferedWriter in Java

BufferedWriter is a class which is available in java.io package. This class is used to write a data to the file more efficiently. BufferWriter class maintains internal buffer of 8192 characters During the write operation, the characters are written to the internal buffer instead of the disk. Once the buffer is filled or the writer is closed, the whole characters in the buffer are written to the disk. Hence, the number of communication to the disk is reduced. This is why writing characters is faster using BufferedWriter.

Create BufferedWriter
  
    // Creates a FileWriter
    FileWriter file = new FileWriter(String name);

    // Creates a BufferedWriter
    BufferedWriter buffer = new BufferedWriter(file);
    // Creates a BufferedWriter with specified size internal buffer, specify the size of the internal buffer
    BufferedWriter buffer = new BufferedWriter(file, int size);
  
BufferedWriter example

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

  public class Main {

    public static void main(String args[]) {

      String data = "This is the data in the output file";

      try {
        // Creates a FileWriter
        FileWriter file = new FileWriter("output.txt");

        // Creates a BufferedWriter
        BufferedWriter output = new BufferedWriter(file);

        // Writes the string to the file
        output.write(data);

        // Closes the writer
        output.close();
      }

      catch (Exception e) {
        e.getStackTrace();
      }
    }
  }

flush() Method

To clear the internal buffer, we can use the flush() method. This method forces the writer to write all data present in the buffer to the destination file.

FileReader in Java

FileReader is a class, which is available in java.io package. Which is used to read the characters from the file.

Create a FileReader

In order to create a file reader, we must import the java.io.FileReader package first. Once we import the package, here is how we can create the file reader. Since Java 11 we can specify the type of character encoding (UTF-8 or UTF-16) in the file as well.

  
    FileReader input = new FileReader(String name); // here name is path
    FileReader input = new FileReader(File aFile); // here aFile is an object
    FileReader input = new FileReader(String file, Charset cs); // file path and character encoding
  
FileReader example

  import java.io.FileReader;

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

      // Creates an array of character
      char[] array = new char[100];

      try {
        // Creates a reader using the FileReader
        FileReader input = new FileReader("input.txt");

        // Reads characters
        input.read(array);
        System.out.println("Data in the file: ");
        System.out.println(array);

        // Closes the reader
        input.close();
      }

      catch(Exception e) {
        e.getStackTrace();
      }
    }
  }

BufferedReader in Java

The BufferedReader class of the java.io package can be used with other readers to read data (in characters) more efficiently. The BufferedReader maintains an internal buffer of 8192 characters. During the read operation in BufferedReader, a chunk of characters is read from the disk and stored in the internal buffer. And from the internal buffer characters are read individually. Hence, the number of communication to the disk is reduced. This is why reading characters is faster using BufferedReader.

Create BufferedReader

  // Creates a FileReader
  FileReader file = new FileReader(String file);

  // Creates a BufferedReader
  BufferedReader buffer = new BufferedReader(file);
  // Creates a BufferdReader with specified size internal buffer, specify the size of the internal buffer
  BufferedReader buffer = new BufferedReader(file, int size);

BufferedReader example

  import java.io.FileReader;
  import java.io.BufferedReader;

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

      // Creates an array of character
      char[] array = new char[100];

      try {
        // Creates a FileReader
        FileReader file = new FileReader("input.txt");

        // Creates a BufferedReader
        BufferedReader input = new BufferedReader(file);

        // Reads characters
        input.read(array);
        System.out.println("Data in the file: ");
        System.out.println(array);

        // Closes the reader
        input.close();
      }

      catch(Exception e) {
        e.getStackTrace();
      }
    }
  }

skip() Method

To discard and skip the specified number of characters, we can use the skip() method.

Delete file in java

  import java.io.File;

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

      // creates a file object
      File file = new File("file.txt");

      // deletes the file
      boolean value = file.delete();
      if(value) {
        System.out.println("The File is deleted.");
      }
      else {
        System.out.println("The File is not deleted.");
      }
    }
  }