A file is used to store certain information. File handling is considered an important aspect of any application. The first and the main step is to create a file and then move towards the coding part. Java is a programming language that performs various operations on files like creation, updation, deletion, etc. We work with files in Java using the java.io package.
In this write-up, we will specifically go through the creation of files in Java and how to write on the created files.
How to Create a File in Java?
In order to create a file in Java, the first step involves the creation of an object and specifying the filename and the directory in which the file is saved. There are numerous methods used by the file class in Java. One of the methods to create a new file is the createNewFile() method in Java. The createNewFile() method returns a boolean value of true if the desired file is made and a value of false if the file is already present.
The code below depicts the creation of a file in Java using the file class.
//Import the required Package
import java.io.File;
class CreateFile {
// main() method of Java
public static void main(String[] args) {
// Declare the location where the file is to be created with its name
File file = new File("D:/Program1.java");
try {
// Declare a file object for the createNewFile method
boolean val = file.createNewFile();
if (val) {
System.out.println("A new file is created in D Drive");
} else {
System.out.println("This file is already present");
}
}
// Exception case in Java
catch (Exception a) {
a.getStackTrace();
}
}
}
In the above Java code:
- The required package to create a file is imported.
- A class is created as “CreateFile” and then a new object of “file” is created in the D drive.
- In the try block, the if-else statement prints the output of whether the file is created or not and whether the file with the same name exists.
- The catch block shows the exception in Java.
Output
The output below shows that the file is created.

How to Write a File in Java?
To write in a file, the FileWriter class of Java is used along with its write() method. This method is used to write a piece of information to the files in Java. When the writing is completed the method is closed with the close() method. The code below depicts the method to write in a file in Java.
//Import the required class
import java.io.FileWriter;
//Declare a class to perform a write operation on a file.
class WriteFile {
// The main() method of Java
public static void main(String args[]) {
// Declare the code in Java using a + operator
String code = "class WriteinFile { " +
"public static void main(String[] args) { " +
"System.out.println(\"How to Write in File in Java\");" +
"System.out.println(\"This file is in D drive\");" +
"}" +
"}";
// The try block creates a writer using the
try {
FileWriter result = new FileWriter("D:/Program1.java");
// The write() method to write to the file
result.write(code);
System.out.println("Java code is declared in the file");
// The writer is closed
result.close();
} catch (Exception e) {
e.getStackTrace();
}
}
}
Output
The output below shows that the code is declared and the code in Program1 is added which is displayed as below:

The added code is in the Program1 file.

Conclusion
To create a file in Java the file name along with its directory name is specified and then the createNewFile() Method is implemented. To write in a file in Java the FileWriter class is executed which is described in detail in this write-up.