Create New File in Java

Create New File in Java is the basic common requirement for each java programmer to store data in text files or the files in other formats.

There are mainly 3 ways to create a new file in Java.

1). Using java.nio.file.Files – Java NIO
2). Using java.io.FileOutputStream class
3). Using java.io.File class 


We will see them one by one

1). Using java.nio.file.Files – Java NIO

The java.nio package contains Files class, using we can create a file. Files class contains a write a method which writes lines of text to a file. Each line is a char sequence and is written to the file in sequence with each line terminated by the line separator, which is platform-independent.

package com.bala.nio.files; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Arrays; import java.util.List; public class CreateNewFileUsingJavaNIO { public static void main(String[] args) { String data = "File is created using Java NIO!!!"; try { Files.write(Paths.get("/home/devuser/hello3.txt"), data.getBytes()); // or List lines = Arrays.asList("File is created using ", "Java NIO.","File Content is updated"); Files.write(Paths.get("/home/devuser/hello3.txt"), lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); System.out.println("TFile is created using Java NIO !!!"); } catch (IOException ex) { System.out.println(ex.getMessage()); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } output: File is created using Java NIO !!!

When you open file hello3.txt you will get content as
File is created using Java NIO!!!File is created using Java NIO. File Content is updated

2). Using java.io.FileOutputStream class

The java.io package contains java.io.FileOutputStream class, using which we can create a file in a given location, and content can be written in the file using the same class.

package com.bala.nio.files; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CreateNewFileUsingFileOutputStream { public static void main(String[] args) { String data = "This is sample text to write in the file."; FileOutputStream out; try { out = new FileOutputStream("/home/devuser/hello1.txt"); out.write(data.getBytes()); out.close(); System.out.println("File is created using FileOutputStream !!!"); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } } output: File is created using FileOutputStream !!!

3). Using java.io.File class

The java.io.File class contains createNewFile() .By using this method we can create a new file of a given location.

File file = new File("home/ubuntu/hello.txt"); //Create the file if (file.createNewFile()){ return true; return false;

but this will only create a file, but not write any content to it. For writing content in the file, we have to use FileWriter.
package com.bala.nio.files; import java.io.*; public class CreateNewFile { public static void main(String[] args) throws IOException { File file = new File("/home/devuser/hello.txt"); // or C:\\hello.txt for Windows try { // create the file in the location /home/devuser/ if (file.createNewFile()) { System.out.println("File is created!"); } else { System.out.println("File already exists."); } // write the data to new file FileWriter writer = new FileWriter(file); writer.write("This is sample text to write in the file."); writer.close(); } catch (IOException ex) { System.out.println(ex.getMessage()); } } } output: File is created!

Post/Questions related to  Create New File in Java

How to check special characters in java using Regex?

How to Download File from URL in Java
Read file to byte[] array in Java Java File Reader Examples
Java file writer Example
java.nio.file.Files methods with Examples
Java File Path, Absolute Path and Relative Path
Path toString() method in Java

In this article, we have seen Create New File in Java with Examples. All source code in the article can be found in the GitHub repository.