File Handling:

File handling in Java allows reading from and writing to files. You can perform various file operations like creating, reading, writing, and deleting on files.

Importing Necessary Package:

You need to import the java.io package to work with file handling in Java.

file.java
import java.io.*;

Creating a File:

file.java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
  
public class CreateFileExample {
  public static void main(String[] args) {
    // Specify the file path
    String filePath = "newfile.txt";
  
    try {
      // Create a File object
      File file = new File(filePath);
  
      // Check if the file already exists
      if (file.exists()) {
        System.out.println("File already exists.");
      } else {
        // Create a new file
        if (file.createNewFile()) {
          System.out.println("File created.");
        } else {
          System.out.println("Failed.");
        }
      }
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}

Reading from a File:

To read data from a file, you can use FileInputStream or other stream classes like BufferedReader.

file.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFromFileExample {
  public static void main(String[] args) {
    // Specify the file path
    String filePath = "sample.txt";

    try(BufferedReader r=new BufferedReader
      (new FileReader(filePath))) {
      String line;
      while ((line = r.readLine()) != null) {
        // Process each line (e.g., print it)
        System.out.println(line);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Writing to a File:

To write data to a file, you can use FileOutputStream or other stream classes like BufferedWriter.

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

public class WriteToFileExample {
  public static void main(String[] args) {
    // Specify the file path
    String filePath = "output.txt";

    try(BufferedWriter w=new BufferedWriter(
      new FileWriter(filePath))) {
      String content = "Hello, World!";
      w.write(content);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Deleting Files:

You can use the File class to delete files in Java.

file.java
import java.io.File;

public class DeleteFileExample {
  public static void main(String[] args) {
    // Specify the file path
    String filePath = "fileToDelete.txt";

    // Create object for the file to be deleted
    File fileToDelete = new File(filePath);

    // Check if the file exists
    if (fileToDelete.exists()) {
      // Attempt to delete the file
      if (fileToDelete.delete()) {
        System.out.println("File deleted.");
      } else {
        System.err.println("Failed.");
      }
    } else {
      System.err.println("File does not exist.");
    }
  }
}

Note : File handling in java allows seamless interaction with files on your computer's file system. The fundamental operations include creating new files, opening existing files, reading from files, and writing to files.