Java Files (Create, Write, Read, Delete)

File handling is an important part of any application. So before we get to know how to read and write to a file in java, first we need to know how to create a file with java.

Create a File:

To create a file in Java, you can use the createNewFile() method. This method returns a boolean value: true if the file was successfully created, and false if the file already exists.


create a file in java

Write to a file:

In the following example, we use the FileWriter class together with its write() method to write some text to the file we created in the example above.


write to a file java

Read a file:

In the following example, we use the Scanner class to read the contents of the text file we created:


read file java

Delete a file:

To delete a file in Java, use the delete() method:

Post a Comment

0 Comments