Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Azndaddy is offline Member
    Join Date
    Mar 2008
    Posts
    24
    Rep Power
    0

    Default [SOLVED] read last line

    Hi, can someone help me out in reading a file, at the moment, i can read a file by calling the command, OPEN.
    i type in the path for the file, eg c:\test.txt that works fine, and in side there is 100 line.

    but instead it start off at line 0 rather then 100.
    I want to be able to read the last line from the text.

    Thanks

    the code i have at the moment are.
    PHP Code:
    String input = getInput();
    
    buffer.setFileName(input);
    buffer.setLine(Test.readFile(input));
    editor.addBuffer(buffer);
    System.out.println("File Name: "+reads.getCurrentBuffer().getLines().size()+ " Lines:)

  2. #2
    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

    Can you post more code segment that anyone can compile and run.

    Seems you want to read a line-by-line?

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    I forget the correct class and I'll be attempting to find it.. unless someone knows it, but you'll want to set read marker so that it positions the stream at line 100 and then read from there on. That is, if I understood you correctly.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Hey Azndaddy.

    Here is a really simple solution for you. This code will read in a .txt file and output the last line only to the console.

    Java Code:
    public static void main(String[] args) throws Exception {
    		
    FileInputStream in = new FileInputStream("file.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    		
    String strLine = null, tmp;
    
    while ((tmp = br.readLine()) != null)
    {
       strLine = tmp;
    }
    		
    String lastLine = strLine;
    System.out.println(lastLine);
    	
    in.close();
    }
    Did this post help you? Please me! :cool:

  5. #5
    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

    Much similar question discussed here later as well. But the users don't want to search the forums. It's not a good habit at all.

    Yes, Don code is the solution. You can do modifications depend on your requirements. But the guideline is this, much simple way this is :)

  6. #6
    Azndaddy is offline Member
    Join Date
    Mar 2008
    Posts
    24
    Rep Power
    0

    Default

    thanks for all feed back, but my assignment was due already.

    And i didn't want to post up more code because that would end up being my whole assignment.

  7. #7
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default Optimized code

    The code given above is not acceptable, if the number of lines in file are in thousands, and we need only last line,
    why should we read the complete file?

    can someone moveon to make it optimized...

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

  9. #9
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Heres a quick question why would you ever want to read from the last line of the code. Why not just put that line at the top.
    My IP address is 127.0.0.1

  10. #10
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default Apache api

    i don' remember the api actually, it was in some apache package. :confused:
    i am the future

  11. #11
    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

    Quote Originally Posted by Zosden View Post
    Heres a quick question why would you ever want to read from the last line of the code. Why not just put that line at the top.
    But Zosden, who know that it's a log file or something like that. How can we change the oder of it.

  12. #12
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    The code given above is not acceptable, if the number of lines in file are in thousands, and we need only last line,
    why should we read the complete file?
    If you look at the original post, Azndaddy said the file was a 100 line file. So this code is more than acceptable.

    Heres a quick question why would you ever want to read from the last line of the code. Why not just put that line at the top.
    This looks like a class assignment to me. He is obviousally learning Java..
    Did this post help you? Please me! :cool:

  13. #13
    Azndaddy is offline Member
    Join Date
    Mar 2008
    Posts
    24
    Rep Power
    0

    Default

    Your Correct DonCash

    I am learning Java, and finding it abit complicated.
    =)

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

  15. #15
    Asset is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    So what is the solution????????????????????



    i'm going to make log for log: its a about 1000 lines and how to take last line fast????????????????????????
    plz HELP!!!
    Last edited by Asset; 10-06-2008 at 02:32 PM.

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

  17. #17
    Asset is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    There are solutions, read the complete thread.
    rrrrrrrr
    Don Cash:

    public static void main(String[] args) throws Exception {

    FileInputStream in = new FileInputStream("file.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    String strLine = null, tmp;

    while ((tmp = br.readLine()) != null)
    {
    strLine = tmp;
    }

    String lastLine = strLine;
    System.out.println(lastLine);

    in.close();
    }


    but if i have 10000 lines it will make more than 10000 operations:(
    its bad..

  18. #18
    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

    Quote Originally Posted by Asset View Post
    but if i have 10000 lines it will make more than 10000 operations:(
    its bad..
    Can you prove that why it's bad?

  19. #19
    Asset is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Can you prove that why it's bad?
    oh sorry, girl, thnks i will use it;)

  20. #20
    ednardo is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default Do forum da Sun: Java Programming [Archive]

    Veja a resposta: Java Programming [Archive] - Re: Reading a file in reverse....

    /************************************************** *****************
    * Author: Ryan D. Emerle
    * Date: 10.12.2004
    * Desc: Reverse file reader. Reads a file from the end to the
    * beginning
    *
    * Known Issues:
    * Does not support unicode!
    ************************************************** *****************/

    package org.emerle.fileIO;
    import java.io.*;
    import java.util.*;

    public class ReverseFileReader {
    private String filename;
    private RandomAccessFile randomfile;
    private long position;

    public ReverseFileReader (String filename) throws Exception {
    // Open up a random access file
    this.randomfile=new RandomAccessFile(filename,"r");
    // Set our seek position to the end of the file
    this.position=this.randomfile.length();

    // Seek to the end of the file
    this.randomfile.seek(this.position);
    //Move our pointer to the first valid position at the end of the file.
    String thisLine=this.randomfile.readLine();
    while(thisLine == null ) {
    this.position--;
    this.randomfile.seek(this.position);
    thisLine=this.randomfile.readLine();
    this.randomfile.seek(this.position);
    }
    }

    // Read one line from the current position towards the beginning
    public String readLine() throws Exception {
    int thisCode;
    char thisChar;
    String finalLine="";

    // If our position is less than zero already, we are at the beginning
    // with nothing to return.
    if ( this.position < 0 ) {
    return null;
    }

    for(;;) {
    // we've reached the beginning of the file
    if ( this.position < 0 ) {
    break;
    }
    // Seek to the current position
    this.randomfile.seek(this.position);

    // Read the data at this position
    thisCode=this.randomfile.readByte();
    thisChar=(char)thisCode;

    // If this is a line break or carrige return, stop looking
    if (thisCode == 13 || thisCode == 10 ) {
    // See if the previous character is also a line break character.
    // this accounts for crlf combinations
    this.randomfile.seek(this.position-1);
    int nextCode=this.randomfile.readByte();
    if ( (thisCode == 10 && nextCode == 13) || (thisCode == 13 && nextCode == 10) ) {
    // If we found another linebreak character, ignore it
    this.position=this.position-1;
    }
    // Move the pointer for the next readline
    this.position--;
    break;
    } else {
    // This is a valid character append to the string
    finalLine=thisChar + finalLine;
    }
    // Move to the next char
    this.position--;
    }
    // return the line
    return finalLine;
    }
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. how to read a form with JFS
    By crispy in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 05-01-2008, 03:07 PM
  2. Read file
    By tajinvillage in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-29-2008, 09:10 AM
  3. How to read the following
    By rrp in forum New To Java
    Replies: 0
    Last Post: 12-03-2007, 06:16 PM
  4. 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
  5. read txt file
    By sureshsri1981 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-05-2007, 03:49 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
  •