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

04-10-2008, 06:22 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 24
|
|
|
[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.
String input = getInput();
buffer.setFileName(input);
buffer.setLine(Test.readFile(input));
editor.addBuffer(buffer);
System.out.println("File Name: "+reads.getCurrentBuffer().getLines().size()+ " Lines:)
|
|

04-10-2008, 07:51 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

04-10-2008, 08:03 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
|
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)
|
|

04-10-2008, 01:01 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 239
|
|
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.
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.
|
|

04-10-2008, 01:04 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
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.
|
|

05-02-2008, 05:17 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 24
|
|
|
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.
|
|

05-02-2008, 10:27 AM
|
 |
Senior Member
|
|
Join Date: Mar 2008
Location: Delhi, India
Posts: 187
|
|
|
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...
|
|

05-02-2008, 11:10 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

05-02-2008, 11:15 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
|
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
|
|

05-02-2008, 11:19 AM
|
 |
Senior Member
|
|
Join Date: Mar 2008
Location: Delhi, India
Posts: 187
|
|
|
Apache api
i don' remember the api actually, it was in some apache package. 
__________________
i am the future
|
|

05-02-2008, 12:09 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
Originally Posted by Zosden
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.
|
|

05-02-2008, 12:33 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 239
|
|
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 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.
|
|

05-02-2008, 01:47 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 24
|
|
|
Your Correct DonCash
I am learning Java, and finding it abit complicated.
=)
|
|

05-02-2008, 01:48 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
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.
|
|

10-06-2008, 04:25 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 3
|
|
|
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.
|
|

10-06-2008, 04:51 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
|
|

10-07-2008, 07:35 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 3
|
|
Originally Posted by Eranga
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..
|
|

10-07-2008, 08:27 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
Originally Posted by Asset
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.
|
|

10-07-2008, 08:39 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 3
|
|
Originally Posted by Eranga
Can you prove that why it's bad?
oh sorry, girl, thnks i will use it 
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
| 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 |
| | |