Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Reading file using Java

Reply
Thread Tools

Reading file using Java

 
 
Hyden Zeng
Guest
Posts: n/a
 
      01-09-2004
Hi all,
got one question regarding file reading.
How do I directly access a certain byte or line of a file using Java?
the time complexity should be O(1).
Thank you.
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      01-09-2004
On 9 Jan 2004 00:18:10 -0800, Hyden Zeng wrote:
> How do I directly access a certain byte or line of a file using
> Java? the time complexity should be O(1).


Open the file using a RandomAccessFile. Use seek() to get to the
desired location. Use an appropriate read method from that class to
get the data.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
Marco Schmidt
Guest
Posts: n/a
 
      01-09-2004
Hyden Zeng:

>got one question regarding file reading.
>How do I directly access a certain byte or line of a file using Java?
>the time complexity should be O(1).


You can access a certain byte by skipping over data with InputStream
or calling the seek method with RandomAccessFile. The complexity
should be O(1). However, extensions of InputStream that do not
override the default implementation of skip(long) will read every byte
before the position you want to go to, so no more O(1) there.

As for accessing a certain line - that's not possible in O(1) without
having some index file which stores the offsets of all lines in a text
file.

Regards,
Marco
--
Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      01-09-2004
Hyden Zeng wrote:

> Hi all,
> got one question regarding file reading.
> How do I directly access a certain byte or line of a file using Java?
> the time complexity should be O(1).


To access a byte address, you can use java.io.RandomAccessFile, there's
probably also an alternative in java.nio

Accessing a certain line in O(1) is fundamentally impossible unless
either line lengths are fixed (in which case a simple computation
reduces it to the earlier problem) or you have a precomputed index
that maps line numbers to byte offsets.

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      01-09-2004
"Hyden Zeng" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
| Hi all,
| got one question regarding file reading.
| How do I directly access a certain byte

RandomAcessFile

|..or line of a file using Java?

If it's FCW, use the formula ((LineNo-1)*LineWidth)
to calculate which byte the line corresponds to,
then RAF.

Otherwise, the best you could do on a non-FCW
file is a binary search, unless you are prepared to
trawl throught he file once, and creat a second
(much smaller) FCW 'index file' for the larger file.

| ..the time complexity should be O(1).

In a perfect world, yes. (I am just guessing
what O(1) means)

| Thank you.

You're welcome..

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-09-2004
On 9 Jan 2004 00:18:10 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (Hyden Zeng) wrote
or quoted :

>got one question regarding file reading.
>How do I directly access a certain byte or line of a file using Java?
>the time complexity should be O(1).


see http://mindprod.com/fileio.html
to read by line.

Then address the char you want with charAt()

Unless you prebuild an index, there is no way to jump to a particular
line. If you prebuild an index, you can then access with random io.
http://mindprod.com/fileio.html
will show you how.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
xml file reading in java /deserialization in java sowmyask Software 0 04-15-2009 07:03 AM
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments