You are currently viewing Java Programming: Creating a New File

Java Programming: Creating a New File

There are various ways in which you can create a new file in Java. In this article, I’ve outlined the two most recommended way of creating new files.

Create New file using Java NIO (Recommended) – JDK 7+

You can use Files.createFile(path) method to create a new File in Java:

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateNewFile {

    public static void main(String[] args)  {
        // New file path
        Path filePath = Paths.get("./bar.txt");

        try {
            // Create a file at the specified file path
            Files.createFile(filePath);
            System.out.println("File created successfully!");

        } catch (FileAlreadyExistsException e) {
            System.out.println("File already exists");
        } catch (IOException e) {
            System.out.println("An I/O error occurred: " + e.getMessage());
        } catch (SecurityException e) {
            System.out.println("No permission to create file: " + e.getMessage());
        }
    }
}

Create New File with missing parent directories using Java NIO

In certain situations, it may be necessary to create any absent parent directories when creating a File in Java. To accomplish this, you can utilize the Files.createDirectories(path) function, which allows you to create any missing parent directories before proceeding with file creation.

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateNewFile {

    public static void main(String[] args)  {
        // New file path
        Path filePath = Paths.get("java/io/bar.txt");

        try {
            // Create missing parent directories
            if(filePath.getParent() != null) {
                Files.createDirectories(filePath.getParent());
            }

            // Create a file at the specified file path
            Files.createFile(filePath);
            System.out.println("File created successfully!");

        } catch (FileAlreadyExistsException e) {
            System.out.println("File already exists");
        } catch (IOException e) {
            System.out.println("An I/O error occurred: " + e.getMessage());
        } catch (SecurityException e) {
            System.out.println("No permission to create file: " + e.getMessage());
        }
    }
}

Create New File in Java using java.io.File class – JDK 6+

Another approach to creating a new File in Java is by using the File.createNewFile() method. This method returns a boolean value, indicating the outcome of the file creation process:

  • If the file does not exist and is successfully created, the method returns true.
  • If the file already exists, the method returns false.
import java.io.File;
import java.io.IOException;

public class CreateNewFile {

    public static void main(String[] args) {
        // Instantiate a File object with a file path
        File file = new File("./foo.txt");

        try {
            // Create the file in the filesystem
            boolean success = file.createNewFile();

            if (success) {
                System.out.println("File created successfully!");
            } else {
                System.out.println("File already exists!");
            }
        } catch (IOException e) {
            System.out.println("An I/O error occurred: " + e.getMessage());
        } catch (SecurityException e) {
            System.out.println("No sufficient permission to create file: " + e.getMessage());
        }
    }
}

Create New File along with missing parent directories with java.io.File class

If your intention is to create any missing parent directories while creating a file, you can achieve that by explicitly calling the file.getParentFile().mkdirs() method. This will create the necessary parent directories for the file.

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

public class CreateNewFile {

    public static void main(String[] args) {
        // Instantiate a File object with a file path
        File file = new File("java/io/foo.txt");

        try {
            // Create missing parent directories
            if(file.getParentFile() != null) {
                file.getParentFile().mkdirs();
            }

            // Create the file
            boolean success = file.createNewFile();

            if (success) {
                System.out.println("File created successfully!");
            } else {
                System.out.println("File already exists!");
            }
        } catch (IOException e) {
            System.out.println("An I/O error occurred: " + e.getMessage());
        } catch (SecurityException e) {
            System.out.println("No sufficient permission to create file: " + e.getMessage());
        }
    }
}

Leave a Reply