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

Program Code in JAVA:
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();
}
}

Program Output:
c:\java>javac FileRead.java
c:\java>java FileRead

1 class FileRead
2 {
3 public static void main(String[] args) throws Exception
4 {
5 File f=new File("FileRead.java");
6 FileReader fr=new FileReader(f);
7 BufferedReader br=new BufferedReader(fr);
8 String str=br.readLine();
9 int i=1;
10 while((str=br.readLine())!=null)
11 {
12 System.out.println(i++ +" "+str);
13 }
14 fr.close();
15 }
16 }
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: