Table of Contents

Java File Handling

The java.io package includes a class known as the File class that provides support for creating files and directories. The class includes several constructors for instantiating the File objects. To use the File class, create an object of the class, and specify the filename or directory name.

Example:

import java.io.File;  // Import the File class

File myObj = new File("filename.txt"); // Specify the filename

1. Creating Files:

To create a file in Java, you can use the createNewFile() method. This method returns a boolean value true if the file was successfully created, and false if the file already exists. It is necessary  to enclose method in try...catch block because it throws an IOException if an error occurs (if the file cannot be created for some reason).

Example:

import java.io.File;  // Import the File class
import java.io.IOException;  // Import the IOException class to handle errors

public class CreateFile {
 public static void main(String[] args) {
   try {
     File f = new File("filename.txt");
     if (f.createNewFile()) {
       System.out.println("File created: " + f.getName());
     } else {
       System.out.println("File already exists.");
     }
   } catch (IOException e) {
     System.out.println("An error occurred.");
     e.printStackTrace();
   }
 }
}

/*OUTPUT:
File created: filename.txt */

2. Writing to the Files:

The FileWriter class together with its write() method to write some text to the file we created. Note that when you are done writing to the file, you should close it with the close() method.

Example:

import java.io.FileWriter;   // Import the FileWriter class
import java.io.IOException;  // Import the IOException class to handle errors

public class WriteToFile {
 public static void main(String[] args) {
   try {
     FileWriter w = new FileWriter("filename.txt");
     w.write("Welcome to java programming");
     w.close();
     System.out.println("Successfully wrote to the file.");
   } catch (IOException e) {
     System.out.println("An error occurred.");
     e.printStackTrace();
   }
 }
}
/*OUTPUT:
Successfully wrote to the file. */

3. Reading Files:

The Scanner class is used to read the contents of the file we created in java. Creating object for Scanner class to fetch and read the file contents.

Example:

import java.io.File;  // Import the File class
import java.io.FileNotFoundException;  // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files

public class ReadFile {
 public static void main(String[] args) {
   try {
     File myObj = new File("filename.txt");
     Scanner myReader = new Scanner(myObj);
     while (myReader.hasNextLine()) {
       String data = myReader.nextLine();
       System.out.println(data);
     }
     myReader.close();
   } catch (FileNotFoundException e) {
     System.out.println("An error occurred.");
     e.printStackTrace();
   }
 }
}
/*OUTPUT:
Welcome to Java Programming */
// Contents written in filename.txt

4. Deleting a File:

In File class delete() method is used to delete the created file in java. The object for File class is created to delete the file in java.

Example:

import java.io.File;  // Import the File class

public class DeleteFile {
 public static void main(String[] args) {
   File f = new File("filename.txt");
   if (f.delete()) {
     System.out.println("Deleted the file: " + f.getName());
   } else {
     System.out.println("Failed to delete the file.");
   }
 }
}
/*OUTPUT:
Deleted the file: filename.txt  */

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?