java-映射内存
发布日期:2021-10-19 19:33:10 浏览次数:2 分类:技术文章

本文共 1122 字,大约阅读时间需要 3 分钟。

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();										}}

转载地址:https://blog.csdn.net/u013494310/article/details/19507973 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java 理论与实践:正确使用Volatile
下一篇:nio-选择器

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月16日 21时50分05秒