Java Create File | New Text File | If Not Exists and examples

Creating a new file in Java is not a big task. There are several ways to do Java to create the file. When you creating a file always give the right path and the name of the file should be easily understood.

Java Create File New Text File If Not Exists and examples

Ways to Create File in Java

Here are 3 ways to do it with used these java classes.

  • File.createNewFile()
  • FileOutputStream class
  • Java NIO

Let’s start examples

We will see different ways to java create a file with examples. There is always a chance of exception so better use IOException and try-catch or exception handling in the program to prevent a crash application.

Create file with – file.createNewFile()

Package is needed to start with File class and method createNewFile() to create Create a New File in Java application. When creating an object of File you have to pass the name of the file with the extension (with path if required).

createNewFile()  if-else statement will return a boolean result- true and false, where true means file created and false means failed to create a file. For that will show a message and check condition in the.

The complete code of an example of How to java create a new file.

Output: New Text File is created!

Gif Image for output and program code structure.

Java Create File example

FileOutputStream class

At a condition where you want a create and write a data into a file using FileOutputStream.write() method automatically create a new file and write content to it.

See the below example of Java create a text file.

Java NIO Files.write()

This is the best approach to create a java file, it’s not needed to close IO resources. You can use the Java NIO Files class to create a new text file and write content into it.

Let’s see the example of it.

How to Java create a file in the directory

The best way to create a file in Directory is –

Conclusion: Some Points you should be in mind when creating a file in java.

  • Check the file if exists or not then create it.
  • Write Program with exception Handling to prevent an application crash
  • Close the file stream after use to release all resources.

Question: How do I create a file and write to it in Java?

Answer: There are 2 approaches to create and write into the file using java programming language, as above mentioned is FileOutputStream class and Java NIO. Follow these ways as above example and explanation.

Do comment if you know more ways, we will add in post. And also if you have any doubt. Some example has checked the condition – create the file if not exists.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Create files are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.|

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.