100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

File Handling in Java: Reading, Writing, and Managing Files with Examples

Rating
-
Sold
-
Pages
5
Uploaded on
23-01-2025
Written in
2024/2025

This document explains file handling in Java, including how to read, write, and manage files using classes like File, FileReader, FileWriter, and BufferedReader. Learn how to work with text and binary files through step-by-step examples. Perfect for second-year Computer Science students.

Show more Read less
Institution
Module









Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Module

Document information

Uploaded on
January 23, 2025
Number of pages
5
Written in
2024/2025
Type
Other
Person
Unknown

Subjects

Content preview

File Handling in Java

1. Working with Files in Java
Java provides several classes for file handling. The most commonly used classes
for file handling are:

 File: Represents the file or directory in the filesystem.
 FileReader, FileWriter: For reading from and writing to text files.
 BufferedReader, BufferedWriter: To improve performance when reading
from and writing to files by buffering the data.
 FileInputStream, FileOutputStream: For reading from and writing to binary
files.

2. The File Class
The File class is used to create, delete, and check file and directory properties. It
can represent both files and directories.

Example:

import java.io.File;

public class Main {
public static void main(String[] args) {
File file = new File("example.txt");

// Check if the file exists
if (file.exists()) {
System.out.println("File exists.");
} else {
System.out.println("File does not exist.");
}

// Create a new file
try {

, if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

3. Reading from Files (FileReader and BufferedReader)
 FileReader is used for reading character files.
 BufferedReader is used to read text from a file efficiently by buffering the
input.

Example (FileReader and BufferedReader):

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Main {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new
FileReader("example.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
£5.32
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
rileyclover179

Get to know the seller

Seller avatar
rileyclover179 US
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
10 months
Number of followers
0
Documents
252
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these revision notes.

Didn't get what you expected? Choose another document

No problem! You can straightaway pick a different document that better suits what you're after.

Pay as you like, start learning straight away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and smashed it. It really can be that simple.”

Alisha Student

Frequently asked questions