Results 1 to 4 of 4

Thread: line number

  1. #1
    kazitula is offline Member
    Join Date
    Oct 2007
    Posts
    10
    Rep Power
    0

    Default line number

    hiya
    i m new in java, i've written a code for file input,its working, but now i need to print the line number, i mean how many line it has printed. i attach the code here.
    try
    {
    fin = new FileInputStream ("c:\\readfile.txt");
    DataInputStream in = new DataInputStream(fin);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    while ((strLine = br.readLine()) != null) {
    System.out.println (strLine);
    }
    fin.close();
    }

    i use catch block for IOException.
    this program can read from file line by line, and i need all the line numbers.
    Thank you

    kazitula

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    try
    {
        fin = new FileInputStream ("c:\\readfile.txt");
        DataInputStream in = new DataInputStream(fin);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        int lineCount = 0;
        while ((strLine = br.readLine()) != null) {
            System.out.println (strLine);
            lineCount++;
        }
        fin.close();
        System.out.println("lineCount = " + lineCount);
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep, simply use a counter variable there just after writing the line. Check the hardwired code carefully.

  4. #4
    kazitula is offline Member
    Join Date
    Oct 2007
    Posts
    10
    Rep Power
    0

    Default

    thanks both of u specially hardwired, u r really helpful, appreciate it.

Similar Threads

  1. how to use Scanner with a number
    By cew27 in forum New To Java
    Replies: 10
    Last Post: 04-03-2009, 06:23 PM
  2. [SOLVED] Line number: ?
    By Azndaddy in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 05:46 AM
  3. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM
  4. how to know the number of the line
    By simon in forum New To Java
    Replies: 3
    Last Post: 08-01-2007, 04:59 PM
  5. Help with setting number as even or odd
    By fegiflu in forum New To Java
    Replies: 8
    Last Post: 07-24-2007, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •