package com.io;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class LargeMappedFiles {
/**
* 内存映射文件允许我们创建和修改那些因为太大而不能放入内存的文件
*/
static int length=0x8FFFFFF;//128MB
public static void main(String[] args) throws IOException {
//public abstract class MappedByteBufferextends ByteBuffer直接字节缓冲区,其内容是文件的内存映射区域。
//映射的字节缓冲区是通过 FileChannel.map 方法创建的。此类用特定于内存映射文件区域的操作扩展 ByteBuffer 类。
MappedByteBuffer out=new RandomAccessFile("test.dat","rw").getChannel().
map(FileChannel.MapMode.READ_WRITE,0,length);
//必须指定映射文件的初始位置和映射区域的长度
for(int i=0;i<length;i++){
out.put((byte)'x');
}
System.out.println("Finish writing");
for(int i=length/2;i<length/2+6;i++){
System.out.println((char)out.get(i));
}
}
}
package com.io;
import java.io.*;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
public class MappedIO {
/**
* 测试内存映射文件的访问速度
*/
private static int numofints=4000000;
private static int numofubuffints=200000;
private abstract static class Tester{
private String name;
public Tester(String name){
this.name=name;
}
//runTest()被用做是一种模板方法,为匿名内部子类中定义的test()的各种实现创建了测试框架
public void runTest() {
System.out.println(name+": ");
long start=System.nanoTime();
try {
test();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long duration=System.nanoTime()-start;
System.out.format("%.2f\n", duration/1.0e9);
}
public abstract void test() throws IOException;
}
private static Tester[] tests={
new Tester("Stream Writer"){
public void test()throws IOException{
DataOutputStream dos=new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(
new File("temp.tmp"))));
for(int i=0;i<numofints;i++){
dos.writeInt(i);
}
dos.close();
}
},new Tester("Mapped Writer"){
public void test()throws IOException{
FileChannel fc=new RandomAccessFile("temp.tmp","rw").getChannel();
IntBuffer ib=fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();//将此通道的文件区域直接映射到内存中。
for(int i=0;i<numofints;i++){
ib.put(i);
}
fc.close();
}
},new Tester("Stream Read"){
public void test()throws IOException{
DataInputStream dis=new DataInputStream(
new BufferedInputStream(new FileInputStream(
"temp.tmp")));
for(int i=0;i<numofints;i++){
dis.readInt();
}
dis.close();
}
},
new Tester("Mapped Read"){
public void test()throws IOException{
FileChannel fc=new FileInputStream(new File("temp.tmp")).getChannel();
IntBuffer ib=fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer();
while(ib.hasRemaining()){
ib.get();
}
fc.close();
}
},
new Tester("Stream Read/Write"){
public void test()throws IOException{
RandomAccessFile raf=new RandomAccessFile(new File("temp.tmp"),"rw");
raf.writeInt(1);
for(int i=0;i<numofubuffints;i++){
raf.seek(raf.length()-4);
raf.writeInt(raf.readInt());
}
raf.close();
}
},
new Tester("Mapped Read/Write"){
public void test()throws IOException{
FileChannel fc=new RandomAccessFile(new File("temp.tmp"),"rw").getChannel();
IntBuffer ib=fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
ib.put(0);
for(int i=1;i<numofubuffints;i++){
ib.put(ib.get(i-1));
}
fc.close();
}
}
};
public static void main(String[] args) throws IOException {
for(Tester test:tests){
test.runTest();
}
/*测试结果如下:Stream Writer:
1.29
Mapped Writer:
0.07
Stream Read:
1.16
Mapped Read:
0.07
Stream Read/Write:
38.49
Mapped Read/Write:
0.01
*/
}
}