0% found this document useful (0 votes)
16 views

File Handling

In Java, a File represents a file or directory path. There are several file operations that can be performed including creating, writing to, reading from, getting information about, and deleting files. Common file operations involve using classes like File, FileWriter, FileReader, and Scanner.

Uploaded by

Navneet Sheoran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

File Handling

In Java, a File represents a file or directory path. There are several file operations that can be performed including creating, writing to, reading from, getting information about, and deleting files. Common file operations involve using classes like File, FileWriter, FileReader, and Scanner.

Uploaded by

Navneet Sheoran
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

File Operations in Java

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.

To get more knowledge about the stream, click here.

Java File Class Methods

S.No Method Return Description


. Type

1. canRead() Boolean The canRead() method is used to check whether we can read the data of the file or
not.

2. createNewFile() Boolean The createNewFile() method is used to create a new empty file.

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.

5. delete() Boolean The delete() method is used to delete a file.

6. getName() String The getName() method is used to find the file name.

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.

10. mkdir() Boolean The mkdir() method is used for creating a new directory.

import java.io.File; // Import the File class

import java.io.IOException; // Import the IOException class to handle errors

public class CreateFile {

public static void main(String[] args) {

try {

File myObj = new File("filename.txt");

if (myObj.createNewFile()) {

System.out.println("File created: " + myObj.getName());

} else {

System.out.println("File already exists.");} }


} catch (IOException e) {

System.out.println("An error occurred.");

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

import java.io.IOException; // Import the IOException class to handle errors

public class WriteToFile {


public static void main(String[] args) {

try {

FileWriter myWriter = new FileWriter("filename.txt");

myWriter.write("Files in Java might be tricky, but it is fun enough!");

myWriter.close();

System.out.println("Successfully wrote to the file.");

} catch (IOException e) {

System.out.println("An error occurred.");

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

import java.io.FileNotFoundException; // Import this class to handle errors

import java.util.Scanner; // Import the Scanner class to read text files

public class ReadFile {

public static void main(String[] args) {

try {

File myObj = new File("filename.txt");

Scanner myReader = new Scanner(myObj);

while (myReader.hasNextLine()) {

String data = myReader.nextLine();

System.out.println(data);

myReader.close();

} catch (FileNotFoundException e) {
System.out.println("An error occurred.");

e.printStackTrace();

Get File Information


To get more information about a file, use any of the File methods:

Example
import java.io.File; // Import the File class

public class GetFileInfo {


public static void main(String[] args) {

File myObj = new File("filename.txt");

if (myObj.exists()) {

System.out.println("File name: " + myObj.getName());


System.out.println("Absolute path: " + myObj.getAbsolutePath());

System.out.println("Writeable: " + myObj.canWrite());

System.out.println("Readable " + myObj.canRead());

System.out.println("File size in bytes " + myObj.length());

} else {

System.out.println("The file does not exist.");

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 {

public static void main(String[] args) {

File myObj = new File("filename.txt");

if (myObj.delete()) {

System.out.println("Deleted the file: " + myObj.getName());

} else {

System.out.println("Failed to delete the file.");

Delete a Folder
You can also delete a folder. However, it must be empty:

Example
import java.io.File;
public class DeleteFolder {

public static void main(String[] args) {

File myObj = new File("C:\\Users\\MyName\\Test");

if (myObj.delete()) {

System.out.println("Deleted the folder: " + myObj.getName());

} else {

System.out.println("Failed to delete the folder.");

You might also like