Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-10-2008, 06:22 AM
Member
 
Join Date: Mar 2008
Posts: 24
Azndaddy is on a distinguished road
[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:) 
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-10-2008, 07:51 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Can you post more code segment that anyone can compile and run.

Seems you want to read a line-by-line?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 08:03 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-10-2008, 01:01 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
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.

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
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-10-2008, 01:04 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-02-2008, 05:17 AM
Member
 
Join Date: Mar 2008
Posts: 24
Azndaddy is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-02-2008, 10:27 AM
rjuyal's Avatar
Senior Member
 
Join Date: Mar 2008
Location: Delhi, India
Posts: 187
rjuyal is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-02-2008, 11:10 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Do you have any suggestion pal.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-02-2008, 11:15 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-02-2008, 11:19 AM
rjuyal's Avatar
Senior Member
 
Join Date: Mar 2008
Location: Delhi, India
Posts: 187
rjuyal is on a distinguished road
Apache api
i don' remember the api actually, it was in some apache package.
__________________
i am the future
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-02-2008, 12:09 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-02-2008, 12:33 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
Quote:
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.

Quote:
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
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 05-02-2008, 01:47 PM
Member
 
Join Date: Mar 2008
Posts: 24
Azndaddy is on a distinguished road
Your Correct DonCash

I am learning Java, and finding it abit complicated.
=)
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 05-02-2008, 01:48 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
It's not complicated. Move step by step. We all are here to help you any time.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 10-06-2008, 04:25 PM
Member
 
Join Date: Oct 2008
Posts: 3
Asset is on a distinguished road
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 04:32 PM.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 10-06-2008, 04:51 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
There are solutions, read the complete thread.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 10-07-2008, 07:35 AM
Member
 
Join Date: Oct 2008
Posts: 3
Asset is on a distinguished road
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..
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 10-07-2008, 08:27 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 10-07-2008, 08:39 AM
Member
 
Join Date: Oct 2008
Posts: 3
Asset is on a distinguished road
Quote:
Originally Posted by Eranga View Post
Can you prove that why it's bad?
oh sorry, girl, thnks i will use it
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to read a form with JFS crispy JavaServer Faces 2 05-01-2008 05:07 PM
Read file tajinvillage Threads and Synchronization 0 01-29-2008 11:10 AM