原来的io已经用新io重新实现过。对于读写文件,看到很多人都推荐使用new io,说什么速度快。我做一下测试。
本测试读写的文件的大小是2M字节多一点。
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
/**
* @ 测试结论:只要缓冲大小设置得当,使用旧io和基本类型写的IO是最快的。
* @author jdgdf566
*/
public class NewMain {
private static int commonIObuffer = 1024 * 32;
/**
* nio内存映射
*
* @throws IOException
*/
public static void mapChannel() throws IOException {
FileInputStream in = new FileInputStream("Java NIO (JSR 51 NIO.1)中文版.pdf");
long size = in.available();
RandomAccessFile out = new RandomAccessFile(Double.toString(Math.random()), &