Java IOjava-io-examples

How to create a new file using Java

Posted On
Posted By admin

In this blog post, I will be demonstrating how you can create a new empty file via Java. Consider the following code snippet:


package learnjava.io;

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

public class CreateFileDemo {

public static void main(String[] args) {
String fileName = "C:/Test2.txt";
File file = new File(fileName);
try {
if (file.createNewFile())
System.out.println("File created successfully");
else
System.out.println("There was an error in creating the file");
} catch (IOException e) {
System.out.println("There was an exception in creating the file:"+e.getMessage());
}

}

}

There is a method called File.createNewFile. This creates a new file corresponding to the path in the File object. It returns a boolean value if the file is created successfully. If there is already a file at the path with the same name or if there is any other issue in creating the file, then this method returns a false. Note that the File.createNewFile can throw an IOException, so this needs to be handled by the code.

When you execute the above code snippet, the following is printed to the console (Assuming there is no file at C:/Test2.txt):

 

File created successfully
If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment