f



Reading file using Java

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.
0
hyden_z (2)
1/9/2004 8:18:10 AM
comp.lang.java.programmer 52711 articles. 1 followers. Post Follow

5 Replies
955 Views

Similar Articles

[PageSpeed] 48

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
0
not108 (1253)
1/9/2004 9:27:10 AM
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
0
marcoschmidt (239)
1/9/2004 9:28:13 AM
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.

0
brazil (1213)
1/9/2004 9:36:40 AM
"Hyden Zeng" <hyden_z@hotmail.com> wrote in message
news:929777ae.0401090018.7393cf1e@posting.google.com...
| 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


0
andrew64 (796)
1/9/2004 2:57:47 PM
On 9 Jan 2004 00:18:10 -0800, hyden_z@hotmail.com (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.
0
1/9/2004 10:35:50 PM
Reply: