Java IO - Create new file

May 16, 2020 No comments Java IO File create Java

1. Introduction

In this article, we are going to present how to create a new file in Java. There are several ways to do it, using core Java IO and NIO methods or external libraries like Apache Commons IO or Google Guava.

This article is a part of the Java IO Tutorial.

2. Create a file with java.io.File

We can create an empty file using File.createNewFile() method available in standard Java I/O API. The method returns boolean value:

  • true - when a file has been created successfully,
  • false - when a file with the given name already exists - in that case, the method will not do anything (it will not override the original content, also timestamp will stay untouched).
package com.frontbackend.java.io;

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

public class CreateFileUsingFile {

    public static void main(String[] args) throws IOException {
        File file = new File("/path/to/file/file1.txt");

        // Create the file
        if (file.createNewFile()) {
            System.out.println("File is created!");
        } else {
            System.out.println("File already exists.");
        }
    }
}

3. Create a file with java.nio.file.Files

JDK 7 gives a new approach to creating files. Java NIO API provides the Files.createFile() method that creates a new file with an empty content. Note that if a file with given name already exists the method will throw FileAlreadyExistsException.

package com.frontbackend.java.io;

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

public class CreateFileUsingFilesFromJavaNIO {

    public static void main(String[] args) throws IOException {
        Path newFilePath = Paths.get("/path/to/file/file2.txt");
        Files.createFile(newFilePath);
    }
}

4. Create a file with java.io.FileOutputStream

Using java.io.FileOutputStream is another indirect method to create a new file in Java. Because the method was not originally designed to create new files, you should use it in situations when you want to fill a newly created file with some input data.

package com.frontbackend.java.io;

import java.io.FileOutputStream;
import java.io.IOException;

public class CreateFileUsingFileOutputStream {

    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("/path/to/file/file3.txt");
        out.close();
    }
}

5. Create a file with Google Guava library

Although it is not recommended if you want to you can use external libraries to create a new file. In Google Guava library you can create a file in a single line using Files.touch() method. The method works similar to touch command in Linux - it creates an empty file or updates the last updated timestamp.

package com.frontbackend.java.io;

import com.google.common.io.Files;

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

public class CreateFileUsingGuava {

    public static void main(String[] args) throws IOException {
        Files.touch(new File("/path/to/file/file4.txt"));
    }
}

6. Create a file with Apache Commons IO library

Apache Commons IO library comes with FileUtils.touch() method that provides the similar functionality to the Guava Files.touch(). Note that if a file already exists it will not throw any exception.

package com.frontbackend.java.io;

import org.apache.commons.io.FileUtils;

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

public class CreateFileUsingCommonsIO {

    public static void main(String[] args) throws IOException {
        FileUtils.touch(new File("/path/to/file/file5.txt"));
    }
}

7. Conclusion

In this tutorial, we described several ways to create an empty file in Java. We use core Java classes available under the java.io and java.nio packages and two external libraries: Guava and Commons IO.

Examples from this article can be found under GitHub repository.

{{ message }}

{{ 'Comments are closed.' | trans }}