Write a Java program that reads a file and displays the file on the screen, with a line number before each line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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(); } } |
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?
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Resources #Terms of Use
Copyright©2012 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com
very simple to learn