Monday, 13 July 2015

Java Programs[Important]


  1.   WRITE A JAVA PROGRAM that reads a file and displays the file on the screen, with a line number before each line.
  2.  
  3. Program :
  4.  
  5. import   java.io.*;
  6.  class LineNum
  7. {
  8.  public static void main(String args[])
  9. String thisline;
  10.  for(int i=0;i<args.length;i++)
  11.  {
  12.  try{ LineNumberReader br=new LineNumberReader(new FileReader(args[i]));
  13.  while((thisline=br.readLine())!=null)
  14.  { 
  15. System.out.println(br.getLineNumber()+"."+thisline); 
  16. }
  17. catch(IOException e)
  18. {
  19.  System.out.println("error:"+e);

  20. }
  21.  }
  22.  }
  23.  }