Working with files in Java

File handling is an important aspect of programming. Java provides a rich set of libraries and tools for working with files easily.

File Class in Java

In Java, the File Class is a part of the java.io package and serves as an abstraction to represent both files and directories. File Class provides an intuitive way to interact with the file system, enabling tasks such as file creation, deletion, querying file attributes, and navigating to the directory.

File f = new File("TheCodeData");

Creating and Checking File

We can initate a File object by providing the file path we can use createNewFile() method to create the actual file.

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

public class FileClass {
    public static void main(String[] args) throws IOException {
        File f = new File("TheCodeData");
        System.out.println(f.exists());
        f.createNewFile();
        System.out.println(f.exists());
    }
}
Creating and Checking File

Note- In UNIX operating system everything is treated as a file. Java file input-output concept is implemented based on the UNIX operating system. Hence Java file objects can be used to represent both files and directories.

File Class Constructor

File f = new File(String name);

It Creates a Java File object to represent the name of the file or directory in the current working directory.

File f = new File(String subDir, String name);
File f = new File(File subDir, String name);

It creates a Java File object to represent the name of the file and directory present in the specified sub-directory (subDir).

Write a program to create a file with the given name in the current working directory

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

public class FileClass {
    public static void main(String[] args) throws IOException {
        File f = new File("TheCodeData");
        f.createNewFile();
    }
}

Write a program to create a directory with name of the code data in the current working directory and create a file java.txt in that directory.

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

public class FileClass {
    public static void main(String[] args) throws IOException {
        File dir = new File("TheCodeData");
      dir.mkdir();  //Creates the directory named by this abstract pathname.
      File f = new File(dir,"java.txt");
      f.createNewFile(); //Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
    }
}

Important Methods in File Class Java

exists() Method

public boolean exists(); 

Tests whether the file or directory denoted by this abstract pathname exists. Return true if a specified file or directory is available.

createNewFile() Method

public boolean createNewFile() throws java.io.IOException

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

mkdir() Method

public boolean mkdir();

Creates the directory named by this abstract pathname. Returns true if and only if the directory was created; false otherwise.

isFile() Method

public boolean isFile();

Return true if the specified file object is pointing to physical file.

isDirectory() Method

public boolean isDirectory();

Return true if the specified file object is pointing to the directory.

list() Method

public String[] list();

This method returns name of all files and sub directory present in the specified directory.

length() Method

public long length();

Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.

delete() Method

public boolean delete();

This method is used to delete specified files or directories.

Similar Java Tutorials

Leave a Comment