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

Write a Java program that reads a file and displays the file on the screen with a line number before each line
 
                            Week – 4
 
b)Aim: To Write a Java program that reads a file and displays the file on the screen with a line number before each line.
Source Code:
 
import java.io.*;
class linenum
{
           public static void main(String[] args)throws IOException
           {
                  FileInputStream fil;
                  LineNumberInputStream line;
                  int i;
                  try
                  {
                  fil=new FileInputStream(args[0]);
                  line=new LineNumberInputStream(fil);
                  }
                  catch(FileNotFoundException e)
                  {
                  System.out.println(“No such file found”);
                  return;
                  }
                  do
                  {
                  i=line.read();
                  if(i==’n’)
                  {
                                                                                                                                            System.out.println();
                                                                                                                                                       System.out.print(line.getLineNumber()+” “);
                  }
           else
                  System.out.print((char)i);
                  }while(i!=-1);
                  fil.close();
                  line.close();
           }
}
 
Expected Output:
 
Demo.java
class Demo
1 {
2         public static void main(java Demo beta gamma delta)
3 {
4 int n = 1 ;
5 System.out.println(“The word is ” + args[ n ] );
6 }
7 }
8 ?

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

( java read file screen line number algorithm source code )

https://en.wikipedia.org/wiki/Command-line_interface

%d bloggers like this: