• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 5 pages
Other

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

Document preview thumbnail
Preview 2 out of 5 pages

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.

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();
}
}
}

Document information

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

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-



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

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions