FileInputStream alone VS BufferedInputStream wrapped FileInputStream

K

Krick

I've been trying to settle on an implementation for a fileCopy method
in one of my utility classes.

After searching the newsgroups, I've narrowed it down to two
implemenations (source for both are below). The first uses
FileInputStream directly, the second wraps the FileInputStream in a
BufferedInputStream. Both methods read in 8K chunks using their own
internal buffers.

My question is this:

Which implemetation is better, and why?


....
Krick





public static void fileCopy1(String src, String dst) {
try {
FileInputStream in = new FileInputStream(new File(src));
FileOutputStream out = new FileOutputStream(new File(dst));
byte[] buffer = new byte[8 * 1024];
int count = 0;
do {
out.write(buffer, 0, count);
count = in.read(buffer, 0, buffer.length);
}
while (count != -1);
in.close();
out.close();
} catch (IOException ex) {}
}

public static void fileCopy2(String src, String dst) {
try {
BufferedInputStream in = new BufferedInputStream(new
FileInputStream(src));
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(dst));
byte[] buffer = new byte[8 * 1024];
int count;
while ((count = in.read(buffer)) >= 0) {
out.write(buffer, 0, count);
}
in.close();
out.close();
} catch (IOException ex) {}
}
 
S

Shripathi Kamath

Krick said:
I've been trying to settle on an implementation for a fileCopy method
in one of my utility classes.

After searching the newsgroups, I've narrowed it down to two
implemenations (source for both are below). The first uses
FileInputStream directly, the second wraps the FileInputStream in a
BufferedInputStream. Both methods read in 8K chunks using their own
internal buffers.

My question is this:

Which implemetation is better, and why?

Benchmarking the two will likely give you the answer that is better for your
app. As far as one can see, the only difference is the buffering which is
usually meant to improve performance.

Of course, the output can be buffered as well.

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top