Click to edit Master title style
File Handling
1 1
ClickHandling
File to edit Master title style
Refers to the manipulation and management of files, and usually involves
these operations:
• Open File
• Read File
• Write to File
• Close File
2 2
Click File
Java to edit
Handling
Master title style
The File class from the java.io package, allows us to work with files. To
use the File class, import the class from the java.io package and create an
object of the class and specify the filename or directory name.
import java.io.File;
File file = new File("FileName.txt");
3 3
Click File
Java to edit
Handling
Master title style
The File class has many useful methods for creating and getting information
about files. For example:
Method Name Type Description
canRead() Boolean Check whether the file is readable or not
canWrite() Boolean Check whether the file is writable or not
createNewFile() Boolean Create an empty file
delete() Boolean Delete file
exists() Boolean Check whether the file exists
getName() String Returns the file name
getAbsolutePath() String Returns the absolute pathname of the file
length() Long Return the size of file in bytes
list() String[] Returns an array of files in the directory
mkdir() Boolean Create directory 4 4
Click Create
Java to edit File
Master title style
To create new file in java, you can use the createNewFile() method in the
File class. This method returns boolean value: true, if the file was created
successfully and false, if the file already exists.
File file = new File("FileName.txt");
if(file.createNewFile()){
// create file
}else{
// file already exists
5 5
}
Click to edit Master title style
import java.io.File;
import java.io.IOException;
// create file
public class FileApp {
public static void main(String[] args) {
try {
File file = new File("FileName.txt");
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException ex) {
System.out.println("An error occurred: " + ex.getMessage());
}
}
}
6 6
Click Create
Java to edit File
Master title style
In the given code snippet, notice that the method is enclosed in a try catch block.
The try catch block is necessary because it throws an IOException if an error
occurs.
7 7
Click to
Write toedit
File Master title style
To write data to a file, we will use the FileWriter class together with its write()
method to write data to the file we created. When you are done writing to the file,
you should close it with the close() method.
FileWriter writer = new FileWriter("FileName.txt");
writer.write("Java example of writing data to file.");
writer.close();
8 8
Click to edit Master title style
import java.io.FileWriter;
import java.io.IOException;
// write to file
public class FileApp {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("FileName.txt");
writer.write("Java example of writing data to file.");
writer.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException ex) {
System.out.println("An error occurred: " + ex.getMessage());
}
}
}
9 9
Click to
Read File
edit Master title style
To read the contents of a file, we need to use the File, FileNotFoundException
class from the java.io package and the Scanner class. We will use the Scanner
class to read the contents of the file. Think of it as getting input data from the
user but this time the input data is from a file.
1010
Click to edit Master title style
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
// read contents from file
public class FileApp {
public static void main(String[] args) {
try {
File file = new File("FileName.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException ex) {
System.out.println("An error occurred: " + ex.getMessage());
}
}
}
1111
Click to edit
Example of Getting
Master File
titleInformation
style
import java.io.File;
public class FileApp {
public static void main(String[] args) {
File file = new File("FileName.txt");
if(file.exists()){
System.out.println("File name: " + file.getName());
System.out.println("Absolute path: " + file.getAbsolutePath());
System.out.println("Writeable: " + file.canWrite());
System.out.println("Readable: " + file.canRead());
System.out.println("File size in bytes: " + file.length());
}else{
System.out.println("File does not exist.");
}
}
}
1212
Click toFile
Delete edit Master title style
To delete file in Java, we will use the delete() method in the File class.
import java.io.File;
public class FileApp {
public static void main(String[] args) {
File file = new File("FileName.txt");
if (file.delete()) {
System.out.println("Deleted File: " + file.getName());
} else {
System.out.println("Failed to delete file.");
}
}
}
1313
Click to edit Master title style
1414