Java-tutorial-Files-created-and-lastModified-and-Size-of-file-method-in-java-with-examples

admin

2/27/2025

Explore Java file handling methods

Go Back

Java Tutorial: Files Created, Last Modified, and Size of File in Java

Updated: 13/Feb/2025 by Computer Hope

File handling is a crucial concept in Java programming. Understanding how to retrieve file details such as creation time, last modified time, and file size helps developers manage and monitor files efficiently. This guide explains these file properties with practical Java examples.

Explore Java file handling methods

Introduction to Java File Handling

Java provides the java.io.File class, which includes methods to check whether a file exists, get its size, and retrieve its last modification date.

Why File Metadata Matters?

  • Helps in monitoring file changes.
  • Useful for tracking file creation and modifications.
  • Essential for log management and debugging.
  • Supports efficient file handling in Java applications.

1. Checking If a File Exists

Before performing operations on a file, it’s essential to check if it exists using the exists() method.

Example Code to Check File Existence

import java.io.File;

public class FileExistExample {
    public static void main(String[] args) {
        File myFile = new File("D:/Assignments/message.txt");
        if (myFile.exists()) {
            System.out.println("File exists: " + myFile.getName());
        } else {
            System.out.println("File does not exist.");
        }
    }
}

Output:

File exists: message.txt

2. Getting the File Size in Java

The length() method returns the file size in bytes.

Example Code to Get File Size

import java.io.File;

public class FileSizeExample {
    public static void main(String[] args) {
        File myFile = new File("D:/Assignments/message.txt");
        if (myFile.exists()) {
            System.out.println("Size of file is: " + myFile.length() + " bytes");
        } else {
            System.out.println("File does not exist.");
        }
    }
}

Output:

Size of file is: 1024 bytes

3. Retrieving the Last Modified Date of a File

The lastModified() method returns the last modification time of a file in milliseconds since the epoch (January 1, 1970, UTC). We can format this using Date class.

Example Code to Get Last Modified Date

import java.io.File;
import java.util.Date;

public class FileLastModifiedExample {
    public static void main(String[] args) {
        File myFile = new File("D:/Assignments/message.txt");
        if (myFile.exists()) {
            System.out.println("File was modified on: " + myFile.lastModified());
            Date lastModified = new Date(myFile.lastModified());
            System.out.println("Formatted date: " + lastModified);
        } else {
            System.out.println("File does not exist.");
        }
    }
}

Output:

File was modified on: 1678923456789
Formatted date: Fri Mar 10 14:10:56 UTC 2025

Best Practices for File Metadata Handling in Java

  • Always check if a file exists before accessing its properties.
  • Convert file timestamps into readable formats for better usability.
  • Handle exceptions properly to prevent unexpected crashes.
  • Use logging to keep track of file changes for debugging and auditing.

Conclusion

Java’s File class provides essential methods to check file existence, retrieve file size, and get the last modified date. Understanding these methods enhances file handling operations in Java applications. By using best practices, developers can efficiently manage file metadata for better performance and security.

Table of content