Java Create and Write Files
We are going to discuss how to create files in Java with examples, in the hopes of meeting your learning objectives.
Java Create Files – TXT
Regarding Java file creation, the createNewFile() method can be used to create a file.
When this method executes, a boolean value is returned: if the file was successfully created, then it returns true, and if it was already existing, then it returns false.
This article illustrates how to create four different types of files using the createnewfile() method.
- .txt file (Text Document File)
- .pdf file (Portable Document Format)
- .csv file (comma-separated values Format)
- .JSON file (JavaScript Object Notation)
A try-catch block surrounds the method. Due to the fact than an IOException will be thrown if there is an error (if the file cannot be created for some reason):
Create txt File Example:
Create txt File Example:2
Creating Java files in a specific directory requires specifying the path and escaping the “” character by double backslashes (for Windows).
On the other hand, you can just write the path, such as: /Users/name/filename.txt, on Macs and Linux.
Create txt File Example:3
Create txt File Example:4
Java Create PDF File
A Portable Document File can be easily created by adding an extension of (.pdf) to the file name while using the java’s createNewFile() method.
The given examples will teach you how to make a pdf file easily:
Create PDF File Example:1
We can also create the .pdf file in any specific folder or directory, as shown by the examples below:
Here we create the .pdf file in public directory using java’s createNewFile() method:
Create PDF File Example:2
Java Create CSV File
Comma Separated Value File can be easily created by adding an extension of (.csv) to the file name while implementing the java’s createNewFile() method.
The given examples will guide you how to do it easily:
Example:
Example:
Given example shows the creation of .csv file in public directory using java’s createNewFile() method:
Example:
Java Create JSON File
Java Script Object Notation file can be formed similarly by adding the extension .json to the file name while constructing it using java createNewFile() method:
Example:
Example:
A .json extension file can be created in a public directory by using Java’s createNewFile() method as shown in the following example:
Example:
Write TXT File In Java
To write some text to the file we created above, we use the FileWriter class and its write() method.
In Java create files, you should close the file after you are finished writing.
We have written and closed the files afterwards, which were created in the examples of creating a file in a specific directory above.
In the example below, we have written successfully to the .txt file:
Example:
Example:
Write PDF File In Java
Similarly, We can also write to the .pdf file we created above in public directory:
Example:
Write CSV File In Java
Given example shows the method of writing to a .csv file:
Example:
Write JSON File In Java
The following example shows how to write to a JSON file:
Write Json File Example:
For more information on reading the files above, please refer to the article on reading files in Java.