The File
class of the java.io
package is used to perform various operations on files and directories.
There is another package named java.nio
that can be used to work with files. However, in this tutorial, we will focus on the java.io
package.
File and Directory
A file is a named location that can be used to store related information. For example,
main.java is a Java file that contains information about the Java program.
A directory is a collection of files and subdirectories. A directory inside a directory is known as subdirectory.
Create a Java File Object
To create an object of File
, we need to import the java.io.File
package first. Once we import the package, here is how we can create objects of file.
// creates an object of File using the path
File file = new File(String pathName);
Here, we have created a file object named file. The object can be used to work with files and directories.
Note: In Java, creating a file object does not mean creating a file. Instead, a file object is an abstract representation of the file or directory pathname (specified in the parenthesis).
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 created a file object named file. The file object is linked with the specified file path.
File file = new File("newFile.txt");
Here, we have used the file object to create the new file with the specified 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 file, we can use subclasses of either InputStream or Reader.
Example: Read a file using FileReader
Suppose we have a file named input.txt with the following content.
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 created an object of FileReader named input. It is now linked with the input.txt file.
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 created a writer using the FileWriter
class. The writer is linked with the output.txt file.
FileWriter output = new FileWriter("output.txt");
To write data to the file, we have used the write()
method.
Here when we run the program, the output.txt file is filled with the following content.
This is the data in the output file.
Java delete files
We can use the delete()
method of the File class to delete the specified file or directory. 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 created an object of File named file. The file now holds the information about the specified file.
File file = new File("file.txt");
Here we have used the delete()
method to delete the file specified by the object.
Also Read: