This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes read specific line from text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "read specific line from text file" Watch "read specific line from text file" New topic
Author

read specific line from text file

Lesole Mphinyane
Ranch Hand

Joined: Oct 07, 2006
Posts: 42
hey all, look at my code hear;


how do i read text file line at num,
num is the random number generated...
pliz help..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Lesole Mphinyane wrote:pliz help..

Please use real words when posting to the forums. Abbreviations such as "pliz" in place of "please" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
You can't randomly read a line from a file (unless each line in the file is of the same length, then you can use a RandomAccesFile), you need to read every line sequentially.

So you could read each line of the file into an ArrayList. Then when you generated the random number you access the line from the ArrayList.
Lesole Mphinyane
Ranch Hand

Joined: Oct 07, 2006
Posts: 42
thanks Bear..
@Rob, um not quite familiar with ArrayLists,
given a text file of 10 lines, how do i read line number 4..?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
Create a loop and read 4 lines.
Lesole Mphinyane
Ranch Hand

Joined: Oct 07, 2006
Posts: 42
Rob Camick wrote:Create a loop and read 4 lines.

i mean reading the fourth line only, is it possible
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
I'm not going to repeat myself, I already gave you the answer to that question!
Lesole Mphinyane
Ranch Hand

Joined: Oct 07, 2006
Posts: 42
Rob Camick wrote:I'm not going to repeat myself, I already gave you the answer to that question!

thanks but i didn't get it clearly.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
thanks but i didn't get it clearly.


What did I say that is so confusing?
Lesole Mphinyane
Ranch Hand

Joined: Oct 07, 2006
Posts: 42
is the anyhow i can do it without using ArrayLists?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Sure. You could use an array. But that would likely be wasteful and inefficient and prone to error unless you know in advance how many lines to read.

What's your beef with ArrayLists?
Lesole Mphinyane
Ranch Hand

Joined: Oct 07, 2006
Posts: 42
Bear Bibeault wrote:Sure. You could use an array. But that would likely be wasteful and inefficient and prone to error unless you know in advance how many lines to read.

What's your beef with ArrayLists?


its not that i have beef with ArrayLists, i cant learn them now.. will try to use the LineNumberReader. how is it
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
Lesole Mphinyane wrote:
its not that i have beef with ArrayLists, i cant learn them now..


What have you been doing for the last hour. It only takes 5 minutes to learn how to use them.

Or you could continue to waste time by trying to use a LineNumberReader, which won't work either.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You can only read a specific line without using a List if you already know how many lines there are. If you don't you may end up trying to read a line that simply isn't there.

As for how List and ArrayList work, it's really simple. In pseudo code:
For more information, read this.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read specific line from text file
 
Similar Threads
Extract all integers from a text file, how to do?
How SortedMap works Internally
vector elements asssignment to variables for storing to db
How can I find out how many lines there are?
Tokens into array, datatype??