package com.java2s.tools.dev;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
class NioBenchmark1 {
static int size = 60; // MB
static int tests = 5;
interface Copier {
public void copy(File s, File t) throws IOException;
}
static class NioCopier implements Copier {
public void copy(File s, File t) throws IOException {
FileChannel in = (new FileInputStream(s)).getChannel();
FileChannel out = (new FileOutputStream(t)).getChannel();
in.transferTo(0, s.length(), out);
in.close();
out.close();
}
}
static class IoCopier implements Copier {
final int BUFF_SIZE = 5 * 1024 * 1024; // 5MB
final byte[] buffer = new byte[BUFF_SIZE];
public void copy(File s, File t) throws IOException {
InputStream in = new FileInputStream(s);
FileOutputStream out = new FileOutputStream(t);
while (true) {
int count = in.read(buffer);
if (count == -1)
break;
out.write(buffer, 0, count);
}
out.close();
in.close();
}
}
public static void main(String[] arg) throws IOException {
Copier io = new IoCopier();
Copier nio = new NioCopier();
nio.copy(new File("s"), new File("t"));
io.copy(new File("s"), new File("t"));
}
}
|