import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class NIOTest {
private static int times = 2000000;
private static String data = "#2015-5-20 14:38:00 revoke() method is invoked...\r\n";;
private static byte[] bytes = data.getBytes();
public static void ensureNewFileExist(String filename) {
File file = new File(filename);
file.deleteOnExit();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.toString());
}
}
public static void fileOutputStream(String filename) throws IOException {
// use fileoutputstream
FileOutputStream fos = new FileOutputStream(filename);
for (int i = 0; i < times; i++) {
fos.write(bytes);
}
fos.flush();
fos.close();
}
public static void bufferedOutputStream(String filename) throws IOException {
// use BufferedOutputStream
FileOutputStream fos = new FileOutputStream(filename);
BufferedOutputStream bos = new BufferedOutputStream(fos);
for (int i = 0; i < times; i++) {
bos.write(bytes);
}
bos.flush();
bos.close();
fos.flush();
fos.close();
}
public static void randomAccessFile(String filename) throws IOException {
// use random access file
RandomAccessFile file = new RandomAccessFile(filename, "rw");
for (int i = 0; i < times; i++) {
file.write(bytes);
}
file.close();
}
public static void mappedFile(String filename) throws IOException {
// use mapped file
// FileOutputStream fos = new FileOutputStream(filename);
RandomAccessFile out = new RandomAccessFile(filename, "rw");
FileChannel channel = out.getChannel();
long size = 200 * 1024 * 1024;
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, size);
for (int i = 0; i < times; i++) {
buffer.put(bytes);
}
channel.close();
out.close();
}
public static void main(String[] args) throws IOException {
// 1 File Output Stream
System.out.println("File Output Stream");
String filename = "./1_file_Output_Stream.txt";
ensureNewFileExist(filename);
long start = System.currentTimeMillis();
fileOutputStream(filename);
long end = System.currentTimeMillis();
System.out.println((end - start) + " milliseconds\n");
// 2 Buffered Output Stream
System.out.println("Buffered File Output Stream");
filename = "./2_buffered_File_Output_Stream.txt";
ensureNewFileExist(filename);
start = System.currentTimeMillis();
bufferedOutputStream(filename);
end = System.currentTimeMillis();
System.out.println((end - start) + " milliseconds\n");
// 3 Random Access File
System.out.println("Random Access Output Stream");
filename = "./3_random_access_Output_Stream.txt";
ensureNewFileExist(filename);
start = System.currentTimeMillis();
randomAccessFile(filename);
end = System.currentTimeMillis();
System.out.println((end - start) + " milliseconds\n");
// 4 Mapped File
System.out.println("Mapped Output Stream");
filename = "./4_mapped_Output_Stream.txt";
ensureNewFileExist(filename);
start = System.currentTimeMillis();
mappedFile(filename);
end = System.currentTimeMillis();
System.out.println((end - start) + " milliseconds");
}
}
------------------------------------------测试结果如下:
看来还是比较快的。