• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

reading file block by block

hi guys,

in delphi or C you can read file block by block to high speed. in java how can i read a huge file with 1024*64 bytes block?

thanks.
0
janavar
Asked:
janavar
1 Solution
 
chandru_inCommented:
If you intend to use the file sequentially, you can use any InputStream subclass.  I'd recommend using BufferedInputStream.

http://java.sun.com/javase/6/docs/api/java/io/BufferedInputStream.html

If you need random access to files, use RandomAccessFile.

http://java.sun.com/javase/6/docs/api/java/io/RandomAccessFile.html
0
 
janavarAuthor Commented:
i am reading data like this;
FileInputStream fis = new FileInputStream(fileName);

int readLen = fis.read(buffer, 0, BUF_LEN)
while (readLen > 0) {
.....
readLen = fis.read(buffer, 0, BUF_LEN)
}

when i set buffer length 62 or 1024*64 it processes the file with the same duration. why? should it be shorter when i use a large buffer? i
0
 
sciuriwareCommented:
When your buffer is a multiple of the physical disk blocks it is always a bit faster,
but it makes no sense to go over 64Kb as you mentioned: few difference.
;JOOP!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
janavarAuthor Commented:
i wonder if how these are equal speed  fis.read() and fis.reaf(buf, 0, 1024)?  if so, FileInputStream already buffers?
fis.read() means goto disk and read a byte and fis.reaf(buf, 0, 1024) means goto disk and read 1024 byte once? and so  fis.reaf(buf, 0, 1024)  must be faster then fis.read()?

 
0
 
sciuriwareCommented:
It is impossible to read 1 byte from a disk; the disk driver will always read blocks.
JAVA is smart enough to read buffered, but a BufferedReader makes pre-processing
possible. That is not of interest with binary data.

;JOOP!
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> fis.reaf(buf, 0, 1024)  must be faster then fis.read()?

No, they need not take the same amount of time to execute. Though it should be faster than 1024 times fis.read ()
0
 
janavarAuthor Commented:
so, if they take  the same duration, is there any solution to faster this? i mean what is the most fast method to read a file?
0
 
sciuriwareCommented:
Compare it to copying it with a filemanager: that's the limit.

;JOOP!
0
 
janavarAuthor Commented:
thanks guys
0
 
sciuriwareCommented:
:)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now