Java Programming Handbook

    Java File Class in Detail

    The java.io.File class is used to represent file and directory pathnames in an abstract manner. It doesn't represent the contents of a file but allows you to create, delete, check properties (like size, existence), and manipulate files or directories.

    1. Constructors of File Class#

    File(String pathname) File(String parent, String child) File(File parent, String child)

    Example:#

    import java.io.File; public class FileConstructorExample { public static void main(String[] args) { // Creating File object using single path File file1 = new File("example.txt"); // Creating File object using parent and child strings File file2 = new File("C:/Users", "example.txt"); // Creating File object using parent File object and child File parentDir = new File("C:/Users"); File file3 = new File(parentDir, "example.txt"); System.out.println(file1.getPath()); System.out.println(file2.getPath()); System.out.println(file3.getPath()); } }

    Output:#

    example.txt C:\\Users\\example.txt C:\\Users\\example.txt

    2. Commonly Used Methods with Examples#

    boolean createNewFile()#

    Creates a new file if it doesn't exist.

    import java.io.File; import java.io.IOException; public class FileCreateExample { public static void main(String[] args) { try { File file = new File("testfile.txt"); if (file.createNewFile()) { System.out.println("File created: " + file.getName()); } else { System.out.println("File already exists."); } } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    File created: testfile.txt

    boolean exists()#

    Checks whether the file exists.

    File file = new File("testfile.txt"); System.out.println("File exists: " + file.exists());

    Output:#

    File exists: true

    boolean delete()#

    Deletes the file or directory.

    File file = new File("testfile.txt"); if (file.delete()) { System.out.println("File deleted successfully."); } else { System.out.println("Failed to delete the file."); }

    Output:#

    File deleted successfully.

    String getName(), String getPath(), String getAbsolutePath()#

    These methods provide various representations of the file path.

    File file = new File("testfile.txt"); System.out.println("Name: " + file.getName()); System.out.println("Path: " + file.getPath()); System.out.println("Absolute Path: " + file.getAbsolutePath());

    Output:#

    Name: testfile.txt Path: testfile.txt Absolute Path: C:\\your\\project\\directory\\testfile.txt

    boolean isDirectory(), boolean isFile()#

    Checks whether it is a directory or file.

    File file = new File("testfile.txt"); System.out.println("Is file? " + file.isFile()); System.out.println("Is directory? " + file.isDirectory());

    Output:#

    Is file? true Is directory? false

    String[] list()#

    Returns the list of files and directories in a directory.

    File dir = new File("."); // Current directory String[] files = dir.list(); if (files != null) { for (String name : files) { System.out.println(name); } }

    Output:#

    FileClassExample.java anotherfile.txt

    long length()#

    Returns the file size in bytes.

    File file = new File("testfile.txt"); System.out.println("File size: " + file.length() + " bytes");

    Output:#

    File size: 25 bytes

    boolean mkdir() and boolean mkdirs()#

    Creates a single directory or multiple directories.

    File dir1 = new File("newFolder"); File dir2 = new File("parentDir/childDir"); System.out.println("Single dir created: " + dir1.mkdir()); System.out.println("Multiple dirs created: " + dir2.mkdirs());

    Output:#

    Single dir created: true Multiple dirs created: true

    3. When to Use the File Class?#

    • Use it when you need to interact with the file system—checking if files/directories exist, creating or deleting files, listing directory contents, etc.
    • It acts as a foundation for many file handling operations and is frequently used with other I/O classes for reading or writing content.

    Conclusion#

    In this blog, we explored the File class in Java, its constructors, and commonly used methods with practical examples and outputs. We saw how it helps us interact with the file system for operations like creation, deletion, and fetching metadata of files or directories.

    In the next blog, we will dive into Reader and Writer classes, which are used for character-based input and output operations.

    Last updated on Apr 09, 2025