Introduction :

Reading and writing text file in Java is a fundamental skill for developers, enabling efficient data handling and file management. Java provides powerful classes like FileReader, FileWriter, BufferedReader, and BufferedWriter to seamlessly perform these operations. Whether you’re dealing with small text files or large data streams, understanding how to read and write text file in Java simplifies tasks like configuration management, logging, or data storage. This article will guide you through practical examples and best practices for working with text files in Java, ensuring you write clean, efficient, and reliable code.

Reading a text File in Java:

A. BufferedReader: 

Let’s have an Example:

[box type=”shadow” align=”” class=”” width=””]BufferedReader in = new BufferedReader(Reader in, int size); [/box]

Program:

import java.io.*; 
public class ReadFile
{ 
 public static void main(String[] arg)throws Exception 
 { 
 
 File fi = new File("C:\\Users\\read\\Desktop\\test.txt"); 
 
 BufferedReader br = new BufferedReader(new FileReader(fi)); 
 
 String st; 
 while ((st = br.readLine()) != null) 
 System.out.println(st); 
 } 
}

B. FileReader Class: It is a class for reading character files.

import java.io.*; 
public class ReadingFile 
{ 
 public static void main(String[] arg) throws Exception 
 { 
 FileReader fileread= new FileReader("C:\\Users\\test\\Desktop\\test.txt"); 
 
 int i; 
 while ((i=fileread.read()) != -1) 
 System.out.print((char) i); 
 } 
}

How it Works:

C. Scanner Class: 

// Java Program to illustrate reading from Text File 
// using Scanner Class 
import java.io.File; 
import java.util.Scanner; 
public class ReadFromFileUsingScanner 
{ 
 public static void main(String[] arg) throws Exception 
 { 
 // pass the path to the file as a parameter 
 File file = new File("C:\\Users\\test\\Desktop\\test.txt"); 
 Scanner sc = new Scanner(file); 
 
 while (sc.hasNextLine()) 
 System.out.println(sc.nextLine()); 
 } 
}

2. Introduction to Writing a text File in Java:

a. Java BufferedWriter Class:

[box type=”shadow” align=”” class=”” width=””]public class BufferedWriter extends Writer [/box]

Program:

import java.io.*; 
public class NewClass 
{ 
 public static void main(String[] args) 
 { 
 //initializing FileWriter 
 FileWriter filewrite; 
 try
 { 
filewrite = new FileWriter("ABC.txt"); 
 
 // Initialing BufferedWriter 
 BufferedWriter bufferwrite = new BufferedWriter(filewrite); 
 System.out.println("Buffered Writer start writing :)"); 
 
 // Use of write() method to write the value in 'ABC' file 
 bufferwrite.write(69); 
 bufferwrite.write(49); 
 
 // Closing BufferWriter to end operation 
 bufferwrite.close(); 
 System.out.println("Written successfully"); 
 } 
 catch (IOException excpt) 
 { 
 excpt.printStackTrace(); 
 } 
 
 } 
}

b) Java FileWriter Class

[box type=”shadow” align=”” class=”” width=””]public class FileWriter extends OutputStreamWriter [/box]

 Program:   

 import java.io.FileWriter; 
 public class FileWriterExample { 
 public static void main(String args[]){ 
 try{ 
 FileWriter fw=new FileWriter("D:\\testout.txt"); 
 fw.write("Welcome"); 
 fw.close(); 
 }
catch(Exception e)
{
System.out.println(e);
} 
 System.out.println("Success..."); 
 } 
 }

Real-World Applications of File Handling

  1. Log Management: Applications generate logs to track errors and performance.
  2. Data Storage: Store application settings and user preferences.
  3. Report Generation: Export reports in text format.
  4. Data Import/Export: Transfer data between systems using files.

File handling is widely used in backend development, making it a must-know for a Java Full Stack Developer.

Conclusion

Mastering file handling in Java is an essential skill for aspiring Java Full Stack Developers. Whether you’re managing logs, storing user data, or working on backend systems, understanding how to read and write text file in java efficiently will set you apart in the industry.

2 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join Free Demo Class

Let's have a chat