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();
}
}
Post A Comment:
0 comments: