Solved

reading file block by block

Posted on 2007-03-28
10
460 Views
Last Modified: 2010-05-18
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
Comment
Question by:janavar
10 Comments
 
LVL 3

Expert Comment

by:chandru_in
ID: 18807066
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
 

Author Comment

by:janavar
ID: 18807724
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 18808425
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
 

Author Comment

by:janavar
ID: 18814460
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
 
LVL 24

Accepted Solution

by:
sciuriware earned 50 total points
ID: 18814727
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Mayank S
ID: 18827679
>> 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
 

Author Comment

by:janavar
ID: 18855823
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 18855827
Compare it to copying it with a filemanager: that's the limit.

;JOOP!
0
 

Author Comment

by:janavar
ID: 19264840
thanks guys
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 19264901
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now