18 Jan Java File Handling
File Handling in Java includes creating, reading, and writing a file. It also includes deleting a file. To work on files in Java, we have the File class. It is part of the following package, which you need to import while working on the File class:
import java.io.File;
Create a File in Java
To create a File in Java, use the createNewFile() method of the File class in Java. First, import the required package:
import java.io.File;
Let’s say the name of the file we want to create is myfile.txt at the following location. Use the double backslash in Windows for the path as shown below:
E:\\Amit\\myfile.txt
Let us now see an example to create a file in Java:
import java.io.File;
import java.io.IOException;
public class Studyopedia {
public static void main(String[] args) {
try {
File f = new File("E:\\Amit\\myfile.txt");
if (f.createNewFile()) {
// Displaying the file name if it gets created successfully
System.out.println("New file created successfully: " + f.getName());
} else {
System.out.println("File "+ f.getName()+ "you want to create already exists.");
}
} catch (IOException e) {
System.out.println("Error!");
e.printStackTrace();
}
}
}
Output
New file created successfully: myfile.txt
The file gets created at the location. The file is empty since we did not add any text to it:

Write to a File in Java
To write to a File in Java, use the write() method of the FileWriter class. First, import the required package:
import java.io.FileWriter;
Let us now see the Java program to write text to the above file myfile.txt:
import java.io.FileWriter;
import java.io.IOException;
public class Studyopedia {
public static void main(String[] args) {
try {
FileWriter fWrite = new FileWriter("E:\\Amit\\myfile.txt");
// The write() method of the FileWriter class writes to a file
fWrite.write("This is a demo text written to a file.");
// Close the file
fWrite.close();
// This gets displayed on screen
System.out.println("File written!!!");
} catch (IOException e) {
System.out.println("Error!");
e.printStackTrace();
}
}
}
Output
File written!!!
The myfile.txt file is now having the following text as shown below:

Read a File in Java
To read a File in Java, use the nextLine() method of the Scanner class. First, import the required package:
import java.io.File; import java.util.Scanner;
We will read the above myfile.txt with the following text:

Let us now see the example to read the above file:
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Studyopedia {
public static void main(String[] args) {
try {
File f = new File("E:\\Amit\\myfile.txt");
// Read the File
System.out.println("Reading the File");
Scanner scanner = new Scanner(f);
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
System.out.println(s);
}
System.out.println("Closing the File");
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("Error!");
e.printStackTrace();
}
}
}
Output
Reading the File This is a demo text written to a file. Closing the File
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments