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) {}
}
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) {}
}