VOOZH about

URL: https://www.geeksforgeeks.org/java/file-handling-in-java/

⇱ File Handling in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

File Handling in Java

Last Updated : 13 Mar, 2026

In Java, file handling means working with files like creating them, reading data, writing data or deleting them. It helps a program save and use information permanently on the computer.

πŸ‘ file_handling_in_java_
File-operations

Why File Handling is Required?

  • To store data permanently instead of keeping it only in memory.
  • To read and write data from/to files for later use.
  • To share data between different programs or systems.
  • To organize and manage large data efficiently.

To support file handling, Java provides the File class in the java.io package.

File Class

File class in Java (from the java.io package) is used to represent the name and path of a file or directory. It provides methods to create, delete, and get information about files and directories.

Output:

File Created!

In Java, I/O streams are used to perform input and output operations on files. So, let’s first understand what streams are.

I/O Streams in Java

In Java, I/O streams are the fundamental mechanism for handling input and output operations. They provide a uniform way to read data from various sources (files, network, memory) and write data to different destinations.

Java I/O streams are categorized into two main types based on the type of data they handle:

1. Byte Streams

In Java, Byte Streams are used to handle raw binary data such as images, audio files, videos or any non-text file. They work with data in the form of 8-bit bytes.

πŸ‘ byte_streams_
Byte Streams

The two main abstract classes for byte streams are:

Since abstract classes cannot be used directly, we use their implementation classes to perform actual I/O operations.

2. Character Streams

In Java, Character Streams are used to handle text data. They work with 16-bit Unicode characters, making them suitable for international text and language support.

πŸ‘ character_streams
Character Stream

The two main abstract classes for character streams are:

  • Reader: Base class for all character-based input streams (reading).
  • Writer: Base class for all character-based output streams (writing).

Since abstract classes cannot be used directly, we use their implementation classes to perform actual I/O operations.

Use Byte Streams when working with binary data (images, audio, video, executable files) and use Character Streams when working with text data (characters, strings, text files).

File Operations

The following are the several operations that can be performed on a file in Java:

1. Create a File

  • In order to create a file in Java, you can use the createNewFile() method.
  • If the file is successfully created, it will return a Boolean value true and false if the file already exists.

Output:

πŸ‘ CreateFile
Output

2. Write to a File

We use the FileWriter class along with its write() method in order to write some text to the file.

Output:

πŸ‘ Writing
Output

3. Read from a File

In Java, the read() method is used with classes like FileReader or InputStream to read data from a file one character or byte at a time.

  • It returns an integer value representing the character or byte read.
  • When the end of the file is reached, the method returns -1 indicating no more data is available.

Output:

πŸ‘ ReadFile
Output

4. canRead a File

canRead() check if the file is readable.

Output:

πŸ‘ out
output

5. canWrite a File

canWrite() checks whether the file can be written to by the program.

Output:

πŸ‘ out2
Output

6. Existance of a File

exists() checks whether the specified file or directory exists on the file system.

Output:

πŸ‘ Out3
Output

7. Getting a path

getAbsolutePath() returns the full path of the file or directory in the file system.

πŸ‘ out4
Output

8. Delete a File

We use the delete() method in order to delete a file.

Output:

πŸ‘ DeleteFile
Output
Comment
Article Tags: