Write a Java program that reads a file and displays the file on the screen, with a line number before each line.

Program Code:
import java.io.*;
class FileRead
{
public static void main(String[] args) throws Exception
{
File f=new File("FileRead.java");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
String str=br.readLine();
int i=1;
while((str=br.readLine())!=null)
{
System.out.println(i++ +" "+str);
}
fr.close();
}
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: