TGViewer
Java Programming Java Programming @java_programming_notes · 33.2K subscribers
Post #1179 1.54K
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

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

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

writer.write("Java File Handling");
writer.newLine();
writer.write("Learning Java");

writer.close();
}
}


🔥 1️⃣0️⃣ Deleting a File

The "delete()" method can be used to delete a file.

import java.io.File;

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

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

if (file.delete()) {
System.out.println("File deleted");
}
}
}


⭐ 1️⃣1️⃣ Important Methods of File

exists() → Checks whether file exists

createNewFile() → Creates a new file

delete() → Deletes file

getName() → Returns file name

length() → Returns file size

isFile() → Checks whether it is a file

isDirectory() → Checks whether it is a directory

Example:

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

System.out.println(file.getName());
System.out.println(file.length());


🔥 1️⃣2️⃣ FileReader vs BufferedReader

FileReader reads characters and can read character by character using read(). It is simpler.

BufferedReader reads text efficiently and can read line by line using readLine(). It is more convenient for text.

🔥 1️⃣3️⃣ FileWriter vs BufferedWriter

FileWriter writes characters, is simple, and uses write().

BufferedWriter uses buffered writing, is more efficient for repeated writes, and uses write() + newLine().

⭐ 1️⃣4️⃣ Why "close()" is Important

After using a file resource, close it.

Example:

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

writer.write("Hello");

writer.close();


Closing the resource helps ensure that data is flushed and the resource is released.

Modern Java often uses try-with-resources, which automatically closes resources.

Example:

try (FileWriter writer = new FileWriter("data.txt")) {
writer.write("Hello Java");
}


👉 No explicit "close()" is required here.

🔥 1️⃣5️⃣ Real-World Example

Imagine a banking application generating a transaction report.

The program could:

Transaction Data
↓
Java Program
↓
Create Report
↓
Write to File
↓
transactions.txt


For example:

try (FileWriter writer = new FileWriter("transactions.txt")) {

writer.write("Transaction ID: 1001\n");
writer.write("Amount: 5000\n");
writer.write("Status: SUCCESS");

}


The resulting file could contain:

Transaction ID: 1001
Amount: 5000
Status: SUCCESS


⭐ Common Interview Questions

Q1. Which class is used to represent a file?

👉 "File"

Q2. Which class can write text to a file?

👉 "FileWriter"

Q3. Which class can read text line by line?

👉 "BufferedReader"

Q4. How do you append data using "FileWriter"?

new FileWriter("data.txt", true);
  • ❤ 3
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, 2026📁 Java File Handling (Important ⭐) File Handling allows Java programs to create, read, wr…
  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 →