File Handling
File Handling
In Java, a File is an abstract data type. A named location used to store related information is known as a File. There are several File
Operations like creating a new File, getting information about File, writing into a File, reading from a File and deleting a File.
Before understanding the File operations, it is required that we should have knowledge of Stream and File methods. If you have
knowledge about both of them, you can skip it.
Stream
A series of data is referred to as a stream. In Java, Stream is classified into two types, i.e., Byte Stream and Character Stream.
File Operations
We can perform the following operation on a file:
o Create a File
o Get File Information
o Write to a File
o Read from a File
o Delete a File
Byte Stream
Byte Stream is mainly involved with byte data. A file handling process with a byte stream is a process in which an input is provided
and executed with the byte data.
Character Stream
Character Stream is mainly involved with character data. A file handling process with a character stream is a process in which an
input is provided and executed with the character data.
1. canRead() Boolean The canRead() method is used to check whether we can read the data of the file or
not.
3. canWrite() Boolean The canWrite() method is used to check whether we can write the data into the file
or not.
4. exists() Boolean The exists() method is used to check whether the specified file is present or not.
7. getAbsolutePath( String The getAbsolutePath() method is used to get the absolute pathname of the file.
)
8. length() Long The length() method is used to get the size of the file in bytes.
9. list() String[] The list() method is used to get an array of the files available in the directory.
try {
if (myObj.createNewFile()) {
} else {
e.printStackTrace();
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. Note that when you are done writing to the file, you should close it with
the close() method:
Example
import java.io.FileWriter; // Import the FileWriter class
try {
myWriter.close();
} catch (IOException e) {
e.printStackTrace();
Read a File
In the previous chapter, you learned how to create and write to a file.
In the following example, we use the Scanner class to read the contents of the text file we created in the previous
chapter:
Example
import java.io.File; // Import the File class
try {
while (myReader.hasNextLine()) {
System.out.println(data);
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
Example
import java.io.File; // Import the File class
if (myObj.exists()) {
} else {
Delete a File
To delete a file in Java, use the delete() method:
Example
import java.io.File; // Import the File class
public class DeleteFile {
if (myObj.delete()) {
} else {
Delete a Folder
You can also delete a folder. However, it must be empty:
Example
import java.io.File;
public class DeleteFolder {
if (myObj.delete()) {
} else {