How do I create a new directory in Java?

We create a directory by calling mkdir() method of the File object. This method returns true if and only if the directory was successfully created. If false was returned, no directory is created. This could be caused, for example that the directory was already exists.

package org.kodejava.io;

import java.io.File;

public class CreateDirectoryExample {
    public static void main(String[] args) {
        File file = new File("tmpdir");

        if (file.mkdir()) {
            System.out.println("Directory = " + file.getAbsolutePath());
        } else {
            System.out.println("No directory was created");
        }
    }
}

Here is the result of the program:

Directory = F:\Wayan\Kodejava\kodejava-example\tmpdir

How do I get the absolute path of a file in Java?

The following code snippet shows you how to get the absolute path of a file. To do this we use the java.io.File object getAbsolutePath() method.

package org.kodejava.io;

import java.io.File;

public class AbsolutePathExample {
    public static void main(String[] args) {
        // Create an instance of File called file.
        File file = new File("README.md");

        // Now we want to know where is exactly this file is located in our file 
        // system. To do this we can use the getAbsolutePath() method of the File
        // class.
        String absolutePath = file.getAbsolutePath();

        // Print out the JavaProgrammingBook.pdf location to the console.
        System.out.println("AbsolutePath = " + absolutePath);
    }
}

Here is the result of the program:

AbsolutePath = F:\Wayan\Kodejava\kodejava-example\README.md

How do I get file size in Java?

This example will show you how to get the size of a file. To obtain the size of a file you can use the File‘s length() method. The length() method return the file size in bytes.

package org.kodejava.io;

import java.io.File;

public class FileSize {
    public static void main(String[] args) {
        File file = new File("README.md");

        // Get the size of a file in bytes.
        long fileSize = file.length();

        // Using Java printf() method to print the file size
        System.out.printf("File size: %,d bytes.%n", fileSize);
    }
}

This will print something like:

File size: 51,981 bytes.

If you want to format the file size in Kilobytes, Megabytes or Gigabytes check the following example How do I create a human-readable file size?.

How do I get file’s last modification date?

To get file last modification date we can use the lastModified() method of the File class. This method returns a long value. After getting this value you can create an instance of java.util.Date class and pass this value as the parameter. This Date will hold the file’s last modification date.

package org.kodejava.io;

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

public class FileLastModificationDate {
    public static void main(String[] args) {
        // Create an instance of file object.
        File file = new File("README.md");
        if (file.exists()) {
            // Get the last modification information.
            long lastModified = file.lastModified();

            // Create a new date object and pass last modified
            // information to the date object.
            Date date = new Date(lastModified);

            // We know when the last time the file was modified.
            System.out.println(date);
        }
    }
}

How do I delete a file in Java?

In this example you will see how to delete a file in Java. To delete a file we start by creating a File object that represent the file to be deleted. We use the exists() method to check if the file is exists before we try to delete it.

To delete the file we call the delete() method of the File object. If the file was successfully deleted the method will return a boolean true result, otherwise it will return false. Let’s try the code snippet below.

package org.kodejava.io;

import java.io.File;

public class FileDeleteExample {
    public static void main(String[] args) {
        // When want to delete a file named readme.txt
        File file = new File("write.txt");

        // Checks if the file is exists before deletion.
        if (file.exists()) {
            System.out.println("Deleting " + file.getAbsolutePath());
            // Use the delete method to delete the given file.
            boolean deleted = file.delete();
            if (deleted) {
                System.out.println(file.getAbsolutePath() + " was deleted.");
            }
        } else {
            System.out.println(file.getAbsolutePath() + " not found.");
        }
    }
}