In this tutorial, we will learn about Java file and their various activities with the help of examples.
The File class of the java.io package is used to perform the different procedures on records and registries.
There is another package named java. nio that can be used to work with records. In any case, in this tutorial, we will focus on the java.io package.
In this article, you will learn-
File and Directory
A file is a named area that can be used to store related data. For instance,
main.java is a Java record that contains data about the Java program.
A catalog is an assortment of files and subdirectories. An index inside a catalog is known as subdirectory.
Make a Java File Object
To make an object of File, we need to import the java.io.File package first. When we import the package, here is the manner by which we can make objects of file.
// creates an object of File using the path
File file = new File(String pathName);
Here, we have made a file object named file. The object can be used to work with records and indexes.
Note: In Java, making a file object doesn’t mean making a record. Instead of considered, a record object is an abstract representation of the file or index pathname (determined in the bracket).
Java File Operation Methods
Operation | Method | Package |
To create file | createNewFile() | java.io.File |
To read file | read() | java.io.FileReader |
To write file | write() | java.io.FileWriter |
To delete file | delete() | java.io.File |
Java create files
To create a new file, we can use the createNewFile() method. It returns
- true if a new file is created.
- false if the file already exists in the specified location.
Example: Create a new File
// importing the File class
import java.io.File;
class Main {
public static void main(String[] args) {
// create a file object for the current location
File file = new File("newFile.txt");
try {
// trying to create a file based on the object
boolean value = file.createNewFile();
if (value) {
System.out.println("The new file is created.");
}
else {
System.out.println("The file already exists.");
}
}
catch(Exception e) {
e.getStackTrace();
}
}
}
In the above example, we have made a file object named file. The file object is connected with the predetermined file path.
File file = new File("newFile.txt");
Here, we have used the file object to make the new file with the predetermined path.
If newFile.txt doesn’t exist in the current location, the file is created and this message is shown.
The new file is created.
However, if newFile.txt already exists, we will see this message.
The file already exists.
Java read files
To read data from the record, we can use subclasses of either InputStream or Reader.
Example: Read a file using FileReader
Assume we have a file named input.txt with the accompanying substance.
This is a line of text inside the file.
Now let’s try to read the file using Java FileReader.
// importing the FileReader class
import java.io.FileReader;
class Main {
public static void main(String[] args) {
char[] array = new char[100];
try {
// Creates a reader using the FileReader
FileReader input = new FileReader("input.txt");
// Reads characters
input.read(array);
System.out.println("Data in the file:");
System.out.println(array);
// Closes the reader
input.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output
Data in the file:
This is a line of text inside the file.
In the above example, we have used made an object of FileReader named input. It is currently connected with the input.txt record.
FileReader input = new FileReader("input.txt");
To read the data from the input.txt file, we have used the read() method of FileReader.
Java write to files
To write data to the file, we can use subclasses of either OutputStream or Writer.
Example: Write to file using FileWriter
// importing the FileWriter class
import java.io.FileWriter;
class Main {
public static void main(String args[]) {
String data = "This is the data in the output file";
try {
// Creates a Writer using FileWriter
FileWriter output = new FileWriter("output.txt");
// Writes string to the file
output.write(data);
System.out.println("Data is written to the file.");
// Closes the writer
output.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
Output
Data is written to the file.
In the above example, we have made a writer using the FileWriter class. The writer is connected with the output.txt file.
FileWriter output = new FileWriter("output.txt");
To write information to the file, we have used the write() technique.
Here when we run the program, the output.txt file is loaded up with the accompanying substance.
This is the data in the output file.
Java delete files
We can use the delete() strategy for the File class to delete the predefined file or registry. It returns
- true if the file is deleted.
- false if the file does not exist.
Note: We can only delete empty directories.
Example: Delete a file
import java.io.File;
class Main {
public static void main(String[] args) {
// creates a file object
File file = new File("file.txt");
// deletes the file
boolean value = file.delete();
if(value) {
System.out.println("The File is deleted.");
}
else {
System.out.println("The File is not deleted.");
}
}
}
Output
The File is deleted.
In the above example, we have made an object of File named file. The record currently holds the data about the predefined file.
File file = new File("file.txt");
Here we have used the delete() method to delete the file specified by the object.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.