Table of Contents
It has been more than 2 decades since Java was launched, and it is still one of the most successful programming languages. Mainly, Java supports various modern functionalities such as sockets, databases, and file handling.
Java offers all the basic functionalities in file handling, such as:
- Create
- Read
- Write
- Delete
Before you begin with any of the file handling operations, you must import the java.io.File
package, and then you have to create a Java file object.
Apart from this, refer to the table below to learn all the basic Java file methods.
Method | Package | Description |
createNewFile() | java.io.File | To create a file |
read() | java.io.FileReader | To read data from the file |
delete() | java.io.File | To delete a file |
write() | java.io.FileWrite | To write data in the file |
With all these methods, you can perform different operations in a file using Java.
Create a file in Java
import java.io.File; class Create { public static void main(String[] args) { // new object of the file type is created File file = new File("new.txt"); try { // a new file will be created boolean value = file.createNewFile(); if (value) { // if the file is created the message will pop System.out.println("The new file is created."); } else { //if the file is already present at the location, the method will pop System.out.println("The file already exists."); } } catch (Exception e) { e.getStackTrace(); } } }
Here in this program, if the file already exists in the directory, then you’ll get the output “The file already exists.” Otherwise, you’ll get “The new file is created.”
Read a file in Java
[adinserter block=”2″]import java.io.FileReader; class Read { public static void main(String[] args) { char[] array = new char[100]; try { // Creates a new reader using the package FileReader FileReader input = new FileReader("fileinput.txt"); // Reads the data from a file in characters. input.read(array); System.out.println("Data in the file:"); System.out.println(array); // terminates the reader and process input.close(); } catch (Exception e) { e.getStackTrace(); } } }
Here a different package is imported to read a file. The FileReader
package is required instead of a File package. The system reads a file name “fileinput.txt” and stores information in a character type array in this program.
Delete a file in Java
import java.io.File; class delete { public static void main(String[] args) { // creates a new file object File file = new File("file.txt"); // to delete the file boolean value = file.delete(); if (value) { System.out.println("The File is successfully deleted."); } else { System.out.println("Sorry! he File is not deleted"); } } }
In this program, first, a file is created, and then it is deleted. You can also delete an existing file. If the file is successfully deleted, the following message will appear “The File is successfully deleted,” whereas you’ll get “Sorry! the File is not deleted”.
Write a File in Java
[adinserter block=”2″]import java.io.FileWriter; class write { public static void main(String args[]) { String data = "This is the data in the output file"; try { // Creates a Writer object using FileWriter package FileWriter outputobj = new FileWriter("Newfile.txt"); // Writing the string in the file outputobj.write(data); System.out.println("Data is written to the file."); // Terminates the writer object outputobj.close(); } catch (Exception e) { e.getStackTrace(); } } }
Here in this program, first, a write function is created using the FileWriter Package. Later the writer object is used to store the String of data in the file (Newfile.txt).
Final Notes
With all these default operations, you will find it extremely easy to handle files using Java. Besides this, you can manipulate anything in the files or can create or delete new files.
Blog Hero