Creating a New File in Java

Learn to create a new file using different techniques including NIO Path, IO File, OutputStream, and open-source libraries such as Guava and Apache commons.

There are separate articles on creating temporary files and making the file read-only.

1. Create New File using Java NIO

The Files.createFile(path, attribs) is the best way to create a new, empty and writable file in Java and it should be your preferred approach in the future if you are not already using it.

  • The createFile() method takes the Path interface instead of the File. It checks if the file already exists, and creates the file thereafter.
  • Checking any existing file and creating the file is done in a single atomic operation.
  • The attribs an optional list of file attributes to set atomically when creating the file.
  • It returns FileAlreadyExistsException If a file of that name already exists.
  • It returns IOException if an I/O error occurs or the parent directory does not exist.

Example 1: Create a new writable file

String TEXT_FILE = "C:/temp/io/textFile.txt";

Path textFilePath = Paths.get(TEXT_FILE);
Files.createFile(textFilePath);

Example 2: Create a new read-only file

Set the file attributes while creating the file. In the given example, we are setting read-only (“r“) access for the owner, group, and others using the string “r–r–r–“.

String TEXT_FILE = "C:/temp/io/textFile.txt";

Set<PosixFilePermission> permissions = PosixFilePermissions
  .fromString("r--r--r--");
FileAttribute<Set<PosixFilePermission>> attribs = PosixFilePermissions
  .asFileAttribute(permissions);

Path textFilePath = Paths.get(TEXT_FILE);
Files.createFile(textFilePath, attribs); 

2. Using File.createNewFile()

Use File.createNewFile() method to create a new file if and only if a file with this name does not yet exist. Checking any existing file and creating the file is an atomic operation.

This method returns a boolean value –

  • true if the file is created successfully.
  • false if the file already exists.
  • IOException If an I/O error occurred.
String TEXT_FILE = "C:/temp/io/textFile.txt";

File textFile = new File(TEXT_FILE);
boolean isFileCreated = textFile.createNewFile(); 

3. Using FileOutputStream

The constructor automatically creates a new file in the given location. Note that if a file with a given name already exists, it will be overwritten.

It throws FileNotFoundException if the given file path represents a directory, or a new file cannot be created for any reason.

String TEXT_FILE = "C:/temp/io/textFile.txt";

try(FileOutputStream fos = new FileOutputStream(TEXT_FILE)){
  // We can write data as byte[]
  // fos.write(data, 0, data.length);
}

4. Guava Files.touch()

To include Guava, add the following to pom.xml.

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.1-jre</version>
</dependency>

The Files.touch() method is similar to the Unix touch command. It creates an empty file or updates the last updated timestamp

The touch command, when used without any option, creates an empty file assuming the file doesn’t exist. If the file exists it changes the timestamp.

String TEXT_FILE = "C:/temp/io/textFile.txt";

com.google.common.io.Files.touch(new File(TEXT_FILE));

5. Apache Commons IO’s FileUtils

To include Apache Commons IO, add the following to pom.xml.

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

The FileUtils.touch() is very similar to the previous example. It also implements the same behavior as the “touch” utility on Unix. 

Also, as from v1.3 this method creates parent directories if they do not exist. It throws an IOException if the last modified date of the file cannot be set.

String TEXT_FILE = "C:/temp/io/textFile.txt";

org.apache.commons.io.FileUtils.touch(new File(TEXT_FILE));

Happy Learning !!

Sourceocde on Github

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.