package UDPDemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class RAMMapped {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
File infile2 = new File ("F:"+File.separator+"maoxiao.txt");
RandomAccessFile infile = new RandomAccessFile(infile2,"rw");
File outfile = new File("F:"+File.separator+"out.txt");
FileOutputStream fot = new FileOutputStream(outfile);
FileChannel ichannel = infile.getChannel();
FileChannel ochannel = fot.getChannel();
MappedByteBuffer inbuf = ichannel.map(FileChannel.MapMode.READ_WRITE, 0, infile.length());//将文件映射到内存
ByteBuffer outbuf = ByteBuffer.allocate((int) infile.length());
ichannel.read(inbuf);
inbuf.flip();
while(inbuf.hasRemaining()){
outbuf.put(inbuf.get());
}
outbuf.flip();
ochannel.write(outbuf);
inbuf.clear();
outbuf.clear();
fot.flush();
ochannel.close();
fot.close();
ichannel.close();
}
}
java-映射内存
最新推荐文章于 2015-06-26 20:05:40 发布