Skip to content
January 18, 2026

uuprogram.com

  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions
Newsletter
Random News
  • Home
  • Java Program
  • Write a Java Program to Read the Content of a File Line by Line
  • Java Program

Write a Java Program to Read the Content of a File Line by Line

Akash Gupta2 months ago03 mins
Java Program to Read the Content of a File Line by Line

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Read the Content of a File Line by Line
Java Program to Read the Content of a File Line by Line

Write a Java Program to Read the Content of a File Line by Line

PROGRAM: Java Program to Read the Content of a File Line by Line

/*Java Program to Read the Content of a File Line by Line*/

1: Java Program to Read File Using BufferedInputStream

import java.io.BufferedInputStream;
import java.io.FileInputStream;

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

// Creates a FileInputStream
FileInputStream file = new FileInputStream(“input.txt”);

// Creates a BufferedInputStream
BufferedInputStream input = new BufferedInputStream(file);

// Reads first byte from file
int i = input .read();

while (i != -1) {
System.out.print((char) i);

// Reads next byte from the file
i = input.read();
}
input.close();
}

catch (Exception e) {
e.getStackTrace();
}
}
}

2: Java Program to Read File Using BufferedReader

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

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

// Creates an array of character
char[] array = new char[100];

try {
// Creates a FileReader
FileReader file = new FileReader(“input.txt”);

// Creates a BufferedReader
BufferedReader input = new BufferedReader(file);

// Reads characters
input.read(array);
System.out.println(“Data in the file: “);
System.out.println(array);

// Closes the reader
input.close();
}

catch(Exception e) {
e.getStackTrace();
}
}
}

3: Java Program to Read File Using Scanner

import java.io.File;
import java.util.Scanner;

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

try {
// create a new file object
File file = new File(“input.txt”);

// create an object of Scanner
// associated with the file
Scanner sc = new Scanner(file);

// read each line from file and print it
System.out.println(“Reading File Using Scanner:”);
while(sc.hasNextLine()) {
System.out.println(sc.nextLine());
}

// close scanner
sc.close();
} catch (Exception e) {
e.getStackTrace();
}
}
}

Tagged: Write a Java Program to Read the Content of a File Line by Line

Post navigation

Previous: Write a Java Program to Load File as InputStream
Next: Write a Java Program to Delete File in Java

Leave a Reply Cancel reply

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

Related News

Java Program to Access elements from a LinkedList.

Write a Java Program to Access elements from a LinkedList.

Akash Gupta2 months ago2 months ago 0
Java Program to Check if two of three boolean variables are true

Write a Java Program to Check if two of three boolean variables are true

Akash Gupta2 months ago 0

Recent Posts

  • Write a Python Program to Sort a Dictionary by Value
  • Write a Python Program to Iterate Over Dictionaries Using for Loop
  • Write a Python Program to Slice Lists
  • Write a Python Program to Flatten a Nested List
  • Write a Python Program to Access Index of a List Using for Loop
No comments to show.

Archives

  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • August 2025
  • July 2025

Categories

  • Array Programs
  • Basic Programs
  • Conditional Programs
  • File handling
  • Java Program
  • Loop Programs
  • Matrix Programs
  • Program in C
  • program in C++
  • Program in python
  • String Programs
  • Uncategorized

Recent Posts

  • Write a Python Program to Sort a Dictionary by Value
  • Write a Python Program to Iterate Over Dictionaries Using for Loop
  • Write a Python Program to Slice Lists
  • Write a Python Program to Flatten a Nested List
  • Write a Python Program to Access Index of a List Using for Loop

Archives

  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • August 2025
  • July 2025

Categories

  • Array Programs
  • Basic Programs
  • Conditional Programs
  • File handling
  • Java Program
  • Loop Programs
  • Matrix Programs
  • Program in C
  • program in C++
  • Program in python
  • String Programs
  • Uncategorized
uuprogram.com 2026. Powered By BlazeThemes.