File Handling in Java: Read, Write, and Manage Text and Binary Files with Practical Examples

File Handling in Java: Read, Write, and Manage Text and Binary Files with Practical Examples




1. Introduction to File Handling in Java

File handling in Java allows programs to create, read, write, and manipulate files on the file system. It is essential for storing data persistently outside the application memory.

Java provides a rich set of classes under java.io and java.nio for file operations.

2. Creating and Writing to Files using FileWriter

The FileWriter class is used to write character data to a file.

Example:

import java.io.FileWriter;
import java.io.IOException; public class WriteFileExample { public static void main(String[] args) { try { FileWriter writer = new FileWriter("example.txt"); writer.write("Hello, Java File Handling!"); writer.close(); System.out.println("File written successfully."); } catch (IOException e) { e.printStackTrace(); } } }

Use BufferedWriter for faster performance when writing large files.

3. Reading Files using FileReader and BufferedReader

To read character data from files, Java provides FileReader. For more efficient reading, wrap it with BufferedReader.

Example:


import java.io.*; public class ReadFileExample { 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); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } }

4. Using File Class for File and Directory Operations

The File class can be used to perform file-level operations like checking existence, creating directories, or deleting files.

Example:


import java.io.File;
public class FileCheckExample { public static void main(String[] args) { File file = new File("example.txt"); if (file.exists()) { System.out.println("File exists."); } else { System.out.println("File does not exist."); } } }

5. Handling Binary Files with FileInputStream and FileOutputStream

Use streams when working with binary files such as images, audio, or videos.

Example:


import java.io.*;
public class BinaryCopyExample { public static void main(String[] args) { try { FileInputStream in = new FileInputStream("source.png"); FileOutputStream out = new FileOutputStream("copy.png"); int byteData; while ((byteData = in.read()) != -1) { out.write(byteData); } in.close(); out.close(); System.out.println("File copied successfully."); } catch (IOException e) { e.printStackTrace(); } } }

6. Best Practices and Error Handling

  • Always close file streams to avoid memory leaks (or use try-with-resources)

  • Use BufferedReader and BufferedWriter for large file operations.

  • Check file existence before reading or writing.

  • Handle exceptions properly to prevent app crashes.


This Content Sponsored by Buymote Shopping app

BuyMote E-Shopping Application is One of the Online Shopping App

Now Available on Play Store & App Store (Buymote E-Shopping)

Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8

Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication



Previous Post Next Post