TGViewer
Java Programming Java Programming @java_programming_notes · 33.2K subscribers
Post #1178 1.13K
📁 Java File Handling (Important ⭐)

File Handling allows Java programs to create, read, write, update, and delete files.

It is useful when your application needs to work with data stored outside the program.

Examples:

• 📄 Reading a text file

• 📝 Writing data to a file

• 📊 Processing CSV files

• 📋 Creating reports

• 💾 Storing application data

✅ 1️⃣ What is File Handling?

File Handling means performing operations on files using Java.

Common operations:

Create

Read

Write

Append

Delete

Java provides several classes for this purpose.

🔹 2️⃣ Important File Handling Classes

Some commonly used classes are:

File

FileReader

FileWriter

BufferedReader

BufferedWriter

Each has a different purpose.

🔹 3️⃣ File Class

The "File" class is used to work with file and directory information.

Example:

import java.io.File;

class FileDemo {
public static void main(String[] args) {

File file = new File("data.txt");

System.out.println(file.exists());
}
}


If "data.txt" exists:

true

Otherwise:

false

🔹 4️⃣ Creating a File

You can create a new file using "createNewFile()".

import java.io.File;
import java.io.IOException;

class CreateFile {
public static void main(String[] args) throws IOException {

File file = new File("data.txt");

if (file.createNewFile()) {
System.out.println("File created");
} else {
System.out.println("File already exists");
}
}
}


🔹 5️⃣ Writing to a File

"FileWriter" can be used to write text into a file.

import java.io.FileWriter;
import java.io.IOException;

class WriteFile {
public static void main(String[] args) throws IOException {

FileWriter writer = new FileWriter("data.txt");

writer.write("Welcome to Java");

writer.close();
}
}


The file will contain:

Welcome to Java


🔹 6️⃣ Appending Data

By default, "FileWriter" can overwrite existing content.

To append instead:

FileWriter writer = new FileWriter("data.txt", true);

writer.write("\nLearning File Handling");

writer.close();


Now the file contains:

Welcome to Java
Learning File Handling


🔹 7️⃣ Reading a File

"FileReader" can read characters from a file.

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

class ReadFile {
public static void main(String[] args) throws IOException {

FileReader reader = new FileReader("data.txt");

int character;

while ((character = reader.read()) != -1) {
System.out.print((char) character);
}

reader.close();
}
}


Output:

Welcome to Java
Learning File Handling


⭐ 8️⃣ BufferedReader

"BufferedReader" is useful for reading text efficiently, especially line by line.

Example:

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

class ReadFile {
public static void main(String[] args) throws IOException {

BufferedReader reader =
new BufferedReader(new FileReader("data.txt"));

String line;

while ((line = reader.readLine()) != null) {
System.out.println(line);
}

reader.close();
}
}


👉 "readLine()" reads one complete line at a time.

🔹 9️⃣ BufferedWriter

"BufferedWriter" can efficiently write text to a file.
  • ❤ 2
More from @java_programming_notes
  1. Sep 23, 2026Important LLM Terms 🔹 Transformer Architecture 🔹 Attention Mechanism 🔹 Pre-training 🔹…
  2. Sep 19, 2026Java projects with tutorial 🚀 Supermarket Management: https://youtube.com/playlist?list=P…
  3. Sep 15, 2026Q5. Why should files be closed? 👉 To release resources and ensure pending data is properl…
  4. Sep 15, 2026import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; clas…
  5. Sep 14, 2026Best YouTube Playlists to learn Programming 👇 1/ Python: https://www.youtube.com/playlist…
  6. Sep 6, 2026When to Use Which Programming Language? C ➝ OS Development, Embedded Systems, Game Engines…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →