调用java hashmap put中的MappedByteBuffer的put方法,是直接写入磁盘么

关于Java共享内存一个疑惑的地方,一定要有对应的磁盘文件么?求大牛指点下
来源:csdn
【RT. 如下代码都是使用了RandomAccessFile 作为对应的磁盘文件,这里不明白的是一定要有磁盘文件么 ?
两个不同的Java进程间共享一块内存,为啥要在磁盘上有对应文件呢?
package com.
import java.io.RandomAccessF
import java.nio.MappedByteB
import java.nio.channels.FileC
import java.nio.channels.FileChannel.MapM
* 往 "共享内存" 写入数据
* @author Unmi
public class WriteShareMemory {
* @param args
* @throws Exception
public static void main(String[] args) throws Exception {
RandomAccessFile raf = new RandomAccessFile("c:/swap.mm", "rw");
FileChannel fc = raf.getChannel();
MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, 1024);
//清除文件内容
for(int i=0;i<1024;i++){
mbb.put(i,(byte)0);
//从文件的第二个字节开始,依次写入 A-Z 字母,第一个字节指明了当前操作的位置
for(int i=65;i<91;i++){
int index = i-63;
int flag = mbb.get(0); //可读标置第一个字节为 0
if(flag != 0){ //不是可写标示 0,则重复循环,等待
mbb.put(0,(byte)1); //正在写数据,标志第一个字节为 1
mbb.put(1,(byte)(index)); //写数据的位置
System.out.println("程序 WriteShareMemory:"+System.currentTimeMillis() +
":位置:" + index +" 写入数据:" + (char)i);
mbb.put(index,(byte)i);//index 位置写入数据
mbb.put(0,(byte)2); //置可读数据标志第一个字节为 2
Thread.sleep(513);
java读共享内存:
package com.
import java.io.RandomAccessF
import java.nio.MappedByteB
import java.nio.channels.FileC
import java.nio.channels.FileChannel.MapM
* 从 "共享内存" 读出数据
* @author Unmi
public class ReadShareMemory {
* @param args
* @throws Exception
public static void main(String[] args) throws Exception {
RandomAccessFile raf = new RandomAccessFile("c:/swap.mm", "rw");
FileChannel fc = raf.getChannel();
MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, 1024);
int lastIndex = 0;
for(int i=1;i<27;i++){
int flag = mbb.get(0); //取读写数据的标志
int index = mbb.get(1); //读取数据的位置,2 为可读
if(flag != 2 || index == lastIndex){ //假如不可读,或未写入新数据时重复循环
lastIndex =
System.out.println("程序 ReadShareMemory:" + System.currentTimeMillis() +
":位置:" + index +" 读出数据:" + (char)mbb.get(index));
mbb.put(0,(byte)0); //置第一个字节为可读标志为 0
if(index == 27){ //读完数据后退出
cloudeagle_bupt:
我的疑惑是,有了磁盘文件,会否影响效率呢?
cloudeagle_bupt:
我的疑惑是,有了磁盘文件,会否影响效率呢?
java中没有专门的共享内存方法。
MappedByteBuffer 是为了文件映射,加快大文件读写速度。
共享内存,有许多种实现方法,在java中可以使用文件映射来实现共享内存,缺点是文件映射必须有文件,同时有其他开销。
文件映射的方式读写文件其实是对内存的操作,所以速度与读写内存是一致的,多余的开销在内存数据还是会同步到硬盘的,这个开销是异步的,影响不大。
cloudeagle_bupt:
有道理,我还有个问题是相比管道而言,进程间数据通信的性能哪个更好? 按照你所说的,开销是异步的话,似乎是内存映射方式效率更高
cloudeagle_bupt:
有道理,我还有个问题是相比管道而言,进程间数据通信的性能哪个更好? 按照你所说的,开销是异步的话,似乎是内存映射方式效率更高
cloudeagle_bupt:
或者说,管道和内存映射方式的进程间通信, 两者其实都是直接在内存上进行操作,所以性能差别不大呢?
Android_iPhone:
磁盘操作肯定没有直接内存操作来得快
单例模式不能满足要求吗?
性能一致。
cloudeagle_bupt:
这个您确定么? 似乎java 采用mmap方式的共享是通过jni调用Linux底层库,因此我觉得应当比socket方式更快些,不需要序列化,网卡等操作。。。。
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动MappedByteBuffer小结 - MappedByteBuffer - Java - ITeye论坛
MappedByteBuffer小结
锁定老帖子
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
等级: 初级会员
来自: 广州
发表时间:&&
相关知识库:
java的内存映射文件有如下特点:
1,使用虚拟内存,因此分配(map)的内存大小不受JVM的-Xmx参数限制,但是也是有大小限制的,首先他理论上不能超过Integer.MAX_VALUE也就是32位操作系统的2G,其次,其实际值在不用操作系统还不一样,在win7 32位操作系统下,他不能超过1.5G,具体多少,没测出来,也不知道什么原因。
2, 对应读大文件,当文件超出1.5G限制是,可以重新MAP下,通过POSITION参数来获取文件后面的内容。
3,它的读取和来回读取要不普通IO快的多,但是单纯的写入还不如普通I/O的一般速度。此结论来自以下测试代码
package com.
import java.io.BufferedOutputS
import java.io.F
import java.io.FileInputS
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
import java.io.RandomAccessF
import java.nio.ByteB
import java.nio.MappedByteB
import java.nio.channels.FileC
import java.nio.channels.FileChannel.MapM
public class FileChannelStudy
static String filename1 = "d:\\work\\code\\filechannelstudy.txt";
static String filename2 = "d:\\work\\code\\file.txt";
static String content = "abcdefghijk\r\n";
static long size =
static long num = size / 10*6;
static long startT = 0;
static long endT = 0;
public static void setStartT()
if(cnt %50 == 0)
System.gc();
System.out.println("call gc");
startT = System.currentTimeMillis();
public static long ellipseT()
endT = System.currentTimeMillis();
long consumeT = endT - startT;
System.out.println("consume time :"+ consumeT/1000 + " second");
return consumeT / 1000;
* @param args
* @throws IOException
public static void main(String[] args) throws IOException
// readFile1();
createFile(true);
preparedFile1();
preparedFile2();
public static void createFile(boolean bReCreate) throws IOException
if(!bReCreate)
File f = new File(filename1);
if(!f.exists()) f.createNewFile();
f = new File(filename2);
if(!f.exists()) f.createNewFile();
File f = new File(filename1);
if(f.exists()) f.delete();
f.createNewFile();
f = new File(filename2);
if(f.exists()) f.delete();
f.createNewFile();
public static void preparedFile2() throws IOException
BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(filename2));
System.out.println("fill file by io");
setStartT();
for (int i = 0; i & i++)
bo.write(content.getBytes());
ellipseT();
if(bo != null) bo.close();
public static void preparedFile1() throws IOException
long mapsize = content.getBytes().length*;
long position = 0;
FileChannel ch = new RandomAccessFile(filename1,"rw").getChannel();
MappedByteBuffer mbb = ch.map(MapMode.READ_WRITE, position, mapsize);
int cnt = 0;
System.out.println("fill file by nio");
setStartT();
for (int i = 0; i & i++)
if(mbb.remaining() & content.getBytes().length)
position += mbb.position();
if(cnt %50 == 0)
System.gc();
System.out.println("call gc");
ch.map(MapMode.READ_WRITE, position, mapsize);
mbb.put(content.getBytes());
ellipseT();
if(ch != null) ch.close();
public static void readFile1() throws IOException
long mapsize = content.getBytes().length*1000000;
long position = 0;
//long rper = ;
long rper =
FileChannel ch = new RandomAccessFile(filename1,"rw").getChannel();
MappedByteBuffer mbb = ch.map(MapMode.READ_WRITE, 0, rper);
int rs = 102400;
byte dst[] = new byte[rs];
int cnt = 0;
while(mbb.hasRemaining())
ByteBuffer bb = mbb.get(dst);
if(cnt %50 == 0) System.out.println(bb.toString());
4,谁然FileOutputStream也有channel功能,但是如果要用内存映射文件方式写文件,则只能使用RandomAccessFile,视乎是因为写时就有读,所以只能用它。
5, 他跟其他ByteBuffer不一样的地方,其他ByteBuffer需要用channel.write/read来写入/读取目标的数据,而MappedByteBuffer直接就是对于目标,它的修改会自动写入到磁盘中,除非你设定了PRIVATE。
6, 内存溢出问题,除了尺寸限制,在写大文件时,由于要不停的重新map,会导致内存溢出,或者说gc来不及回收内存,如上面程序,如果把prepareFile1中的
if(cnt %50 == 0)
System.gc();
System.out.println("call gc");
代码删除,则在3G左右就会报内存溢出;如果只保留mbb=则在5G左右报内存溢出,都保留则不报内存溢出。因此需要手工运行System.gc().
7, 对于中文读写,需要转码。
当然原来io也需要转码,不过有InputStreamReader中可以指定字符集因此可以不自己写代码。
如果不转码,则用UE等工具打开文件看到的是乱码,但是用java的MappedByteBuffer读取处理还是中文。
转码代码:
public static ByteBuffer getBytes(String str)
{// 将字符转为字节(编码)
Charset cs = Charset.forName("GBK");
ByteBuffer bb = ByteBuffer.wrap(str.getBytes(cs));
public static String getChars(ByteBuffer bb)
{// 将字节转为字符(解码)
Charset cs = Charset.forName("GBK");
bb.flip();
CharBuffer cb = cs.decode(bb);
return cb.toString();
跳转论坛:移动开发技术
Web前端技术
Java企业应用
编程语言技术用户名:ilex
文章数:234
评论数:47
访问量:347768
注册日期:
阅读量:1297
阅读量:3317
阅读量:461426
阅读量:1145802
[匿名]51cto游客:
51CTO推荐博文
目的: &&& 有时候用U盘转移大文件时,U盘空间太小,想写一个小程序能把文件分割方便转移。
问题一:用Buffer直接将文件(1G以上)映入内存,不可能直接进物理内存。 &&& 解决方案:将文件用Map映射到一个MappedByteBuffer中
问题二:报告“java.io.IOException: 存储空间不足,无法处理此命令。”异常。 &&& 解决方案:将文件分段映射。注意文件较大时必须显式调用 &&&&&&&&&&&&& System.gc(); &&&&&&&&&&& System.runFinalization(); &&&&&&&&&&& 否则还是可能报上述异常。 &&&&&&& 原因:《JAVA编程思想》4.3 清除:收尾和垃圾收集& 有如下说明: &&&&&&&&&&&&& 当然,Java可用垃圾收集器回收由不再使用的对象占据的内存。现在考虑 &&&&&&&&&&&&& 一种非常特殊且不多见的情况。假定我们的对象分配了一个“特殊”内存区 &&&&&&&&&&&&& 域,没有使用new。垃圾收集器只知道释放那些由new分配的内存,所以不知 &&&&&&&&&&&&& 道如何释放对象的“特殊”内存。为解决这个问题,Java提供了一个名为 &&&&&&&&&&&&& finalize()的方法,可为我们的类定义它。
问题三:原文件不可被删除(在程序退出之前)。 &&& 解决方案:JDK API 1.60中有“映射的字节缓冲区和它所表示的文件映射关系在该缓冲 &&&&&&&&&&&&& 区本身成为垃圾回收缓冲区之前一直保持有效。 ”关于此问题众说纷纭,并 &&&&&&&&&&&&& 且在sun.bug中也在讨论。笔者认为只要最后把映射指向一个无关的文件即可 &&&&&&&&&&&&& 解决。
关键代码: &&&&&&&&&&&&& frame.jTextField6.getText()& //待分割的文件 &&&&&&&&&&&&& frame.jTextField5.getText()+"() //保存位置 &&&&&&&&&&&&& findex&& //方便以后组合
try{&&&& &&&&&&&&&&&&&&&&&&&&&&&&File fcut = new File(frame.jTextField6.getText());&&&& &&&&&&&&&&&&&&&&&&&&&&&&File fsave = new File(frame.jTextField5.getText()+"\\"+this.findname());&&&& &&&&&&&&&&&&&&&&&&&&&&&&if(!fsave.exists()){&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&fsave.mkdir();&&&& &&&&&&&&&&&&&&&&&&&&&&&&}&&&& &&&&&&&&&&&&&&&&&&&&&&&&File findex = new File(frame.jTextField5.getText()+"\\"+this.findname()+"\\cut.index");&&&& &&&&&&&&&&&&&&&&&&&&&&&&FileWriter fw = new FileWriter(findex);&&&& &&&&&&&&&&&&&&&&&&&&&&&&BufferedWriter bw = new BufferedWriter(fw);&&&& &&&&&&&&&&&&&&&&&&&&&&&&bw.write(this.findname()+"."+this.findkuozhan(),0,(this.findname()+"."+this.findkuozhan()).length());&&&& &&&&&&&&&&&&&&&&&&&&&&&&bw.newLine();&&&& &&&& &&&&&&&&&&&&&&&&&&&&&&&&int num = size_i;&&&&//分割份数&&&& &&&&&&&&&&&&&&&&&&&&&&&&long totalsize = fcut.length();&&&& //文件总大小&&&& &&&&&&&&&&&&&&&&&&&&&&&&long persize = totalsize/&&&&&&&& //每个大小&&&& &&&& &&&&&&&&&&&&&&&&&&&&&&&&FileInputStream fis = new FileInputStream(fcut);&&&& &&&&&&&&&&&&&&&&&&&&&&&&FileChannel fic = fis.getChannel();&&&& &&&& &&&&&&&&&&&&&&&&&&&&&&&&if(persize&buffersize){&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&for(int i=0;i&i++){&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&RandomAccessFile ras = new RandomAccessFile(frame.jTextField5.getText()+"\\"+this.findname()+"\\"+this.findname()+".cut"+(i+1),"rw");&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&FileChannel frs = ras.getChannel();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if(i&num-1){&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&for (int j = 0; j & persize / j++) {&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if (j & persize / buffersize - 1) {&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mibb = fic.map(FileChannel.MapMode.READ_ONLY,i * persize + j * buffersize,buffersize);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mobb = frs.map(FileChannel.MapMode.READ_WRITE,j*buffersize,buffersize);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.put(mibb);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mibb.clear();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.clear();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}else{&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mibb = fic.map(FileChannel.MapMode.READ_ONLY,i * persize + j * buffersize,buffersize+persize%buffersize);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mobb = frs.map(FileChannel.MapMode.READ_WRITE,j*buffersize,buffersize+persize%buffersize);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.put(mibb);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mibb.clear();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.clear();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.gc();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.runFinalization();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&bw.write(frame.jTextField5.getText()+"\\"+this.findname()+"\\"+this.findname()+".cut"+(i+1),0,(frame.jTextField5.getText()+"\\"+this.findname()+"\\"+this.findname()+".cut"+(i+1)).length());&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&bw.newLine();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}else{&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&for (int j = 0; j & persize / j++) {&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if (j & persize / buffersize - 1) {&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mibb = fic.map(FileChannel.MapMode.READ_ONLY,i * persize + j * buffersize,buffersize);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mobb = frs.map(FileChannel.MapMode.READ_WRITE,j*buffersize,buffersize);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.put(mibb);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mibb.clear();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.clear();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}else{&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mibb = fic.map(FileChannel.MapMode.READ_ONLY,i * persize + j * buffersize,totalsize-(i * persize + j * buffersize));&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mobb = frs.map(FileChannel.MapMode.READ_WRITE,j*buffersize,totalsize-(i * persize + j * buffersize));&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.put(mibb);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mibb.clear();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.clear();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.gc();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.runFinalization();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&bw.write(frame.jTextField5.getText()+"\\"+this.findname()+"\\"+this.findname()+".cut"+(i+1),0,(frame.jTextField5.getText()+"\\"+this.findname()+"\\"+this.findname()+".cut"+(i+1)).length());&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&bw.close();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&fw.close();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&ras.close();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&frs.close();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&fis.close();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&fic.close();&&&& &&&& &&&&&&&&&&&&&&&&&&&&&&&&}else{&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&for(int i=0;i&i++){&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&RandomAccessFile ras = new RandomAccessFile(frame.jTextField5.getText()+"\\"+this.findname()+"\\"+this.findname()+".cut"+(i+1),"rw");&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&FileChannel frs = ras.getChannel();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if(i&num-1){&&&& &&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mibb = fic.map(FileChannel.MapMode.READ_ONLY,i * persize,persize);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mobb = frs.map(FileChannel.MapMode.READ_WRITE,0,persize);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.put(mibb);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mibb.clear();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.clear();&&&& &&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.gc();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.runFinalization();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&bw.write(frame.jTextField5.getText()+"\\"+this.findname()+"\\"+this.findname()+".cut"+(i+1),0,(frame.jTextField5.getText()+"\\"+this.findname()+"\\"+this.findname()+".cut"+(i+1)).length());&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&bw.newLine();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}else{&&&& &&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mibb = fic.map(FileChannel.MapMode.READ_ONLY,i * persize,totalsize-i * persize);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&MappedByteBuffer mobb = frs.map(FileChannel.MapMode.READ_WRITE,0,totalsize-i * persize);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.put(mibb);&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mibb.clear();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mobb.clear();&&&& &&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.gc();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.runFinalization();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&bw.write(frame.jTextField5.getText()+"\\"+this.findname()+"\\"+this.findname()+".cut"+(i+1),0,(frame.jTextField5.getText()+"\\"+this.findname()+"\\"+this.findname()+".cut"+(i+1)).length());&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&bw.close();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&fw.close();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&ras.close();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&frs.close();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&fis.close();&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&fic.close();&&&& &&&& &&&& &&&&&&&&&&&&&&&&&&&&&&&&}&&&& &&&&&&&&&&&&&&&&jop = new JOptionPane();&&&& &&&&&&&&&&&&&&&&jop.showMessageDialog(frame.getContentPane(),"文件分割完毕!", "成功", JOptionPane.ERROR_MESSAGE);&&&& &&&&&&&&&&&&&&&&}catch(Exception e){&&&& &&&&&&&&&&&&&&&&&&&&&&&&jop = new JOptionPane();&&&& &&&&&&&&&&&&&&&&&&&&&&&&jop.showMessageDialog(frame.getContentPane(),e.toString(), "错误", JOptionPane.ERROR_MESSAGE);&&&& &&&&&&&&&&&&&&&&}&&&&
了这篇文章
类别:┆阅读(0)┆评论(0)& *MappedByteBuffer的创建
& 在FileChannel上调用map方法 返回一个MappedByteBuffer对象&&
public MappedByteBuffer map(MapMode mode, long position, long size)
&&&MapMode& 映射模式(MapMode 是FileChannel中的一个内部类) 有三个可选&#20540;
&& 1.READ_ONLY&&& 只读映射模式
&& 2.READ_WRITE& 读/写映射模式
&& 3.PRIVATE&&&&&&&&&& 通过put方法对MappedByteBuffer的修改&& 不会修改到磁盘文件& 只是虚拟内存的修改
*MappedByteBuffer在父类ByteBuffer的基础上 新增的几个方法
&& 1.fore缓冲区在READ_WRITE模式下,此方法对缓冲区所做的内容更改强制写入文件
&& 2.load:将缓冲区的内容载入物理内存,并返回该缓冲区的引用
&& 3.isLoaded:判断缓冲区的内容是否在物理内存,如果在则返回true,否则返回false
private final static Charset charset = Charset.forName(&GBK&);
* &br&------------------------------&br&
* @param path
* @throws IOException
private static String read(String path) throws IOException {
if (path == null || path.length() == 0)
FileInputStream fileInputStream =
new FileInputStream(path);
FileChannel fileChannel =
fileInputStream.getChannel();
MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.READ_ONLY, 0, fileChannel.size());
fileInputStream.close();
fileChannel.close();
String str = charset.decode(mappedByteBuffer).toString();
mappedByteBuffer =
* 追加内容
* &br&------------------------------&br&
* @param path
* @param str
* @throws IOException
private static MappedByteBuffer append(String path, String str) throws IOException {
if (str == null || str.length() == 0)
RandomAccessFile randomAccessFile = new RandomAccessFile(path, &rw&);
FileChannel fileChannel = randomAccessFile.getChannel();
byte[] bytes = str.getBytes();
long size = fileChannel.size() + bytes.
MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.READ_WRITE, 0, size);
fileChannel.close();
randomAccessFile.close();
int position = mappedByteBuffer.limit() - bytes.
mappedByteBuffer.position(position);
mappedByteBuffer.put(bytes);
mappedByteBuffer.force();
mappedByteBuffer.flip();
return mappedByteB
* 文件复制
* &br&------------------------------&br&
* @param srcfilePath
* @param targetPath
* @throws IOException
private static void copy(String srcfilePath, String targetPath) throws IOException {
File file = new File(targetPath);
if (!file.getParentFile().exists()) {
file.mkdirs();
RandomAccessFile inRandomAccessFile = new RandomAccessFile(srcfilePath, &r&);
FileChannel inFileChannel = inRandomAccessFile.getChannel();
MappedByteBuffer inMappedByteBuffer = inFileChannel.map(MapMode.READ_ONLY, 0, inFileChannel.size());
inRandomAccessFile.close();
inFileChannel.close();
RandomAccessFile outRandomAccessFile = new RandomAccessFile(targetPath, &rw&);
FileChannel outFileChannel = outRandomAccessFile.getChannel();
MappedByteBuffer outMappedByteBuffer = outFileChannel.map(MapMode.READ_WRITE, 0, inMappedByteBuffer.capacity());
outMappedByteBuffer.put(inMappedByteBuffer);
outMappedByteBuffer.force();
outRandomAccessFile.close();
outFileChannel.close();
outMappedByteBuffer.flip();
* 不会更新到文件 只会更新虚拟内存
* &br&------------------------------&br&
* @param path
* @param str
* @throws IOException
private static MappedByteBuffer doTestPrivateMode(String path, String str) throws IOException {
if (str == null || str.length() == 0)
RandomAccessFile randomAccessFile = new RandomAccessFile(path, &rw&);
FileChannel fileChannel = randomAccessFile.getChannel();
byte[] bytes = str.getBytes();
long size = fileChannel.size() + bytes.
MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.PRIVATE, 0, size);
mappedByteBuffer.put(bytes);
fileChannel.close();
randomAccessFile.close();
mappedByteBuffer.flip();
System.out.println(charset.decode(mappedByteBuffer));
return mappedByteB
本文已收录于以下专栏:
相关文章推荐
网上很多IO资料,对新手来说,越看越晕。根据自己的理解,总结对比了一下BIO、NIO、AIO。
BIO:线程发起IO请求,不管内核是否准备好IO操作,从发起请求起,线程一直阻塞,直到操作...
MappedByteBuffer的内存释放,主要由垃圾回收引起的。
首先,来看一下Oracle的bug list,这是一个无法修复的bug,所以在使用MappedByteBuffer的时候一定要注意...
MappedByteBuffer是java nio引入的文件内存映射方案,读写性能极高。NIO最主要的就是实现了对异步操作的支持。其中一种通过把一个套接字通道(SocketChannel)注册到一个选...
之前由于种种琐事,暂停了这个翻页效果的实现,终于在这周末完成了大部分功能,但是这里只是给出了一个基本的雏形,没有添加翻页的动画效果,由于下个周末开始,需要转向去研究framework层(短暂的酱油期就...
其实掌握MappedByteBuffer并不难,只要记住“三方三法三特性”(我自己总结的,呵呵~~不要扔鸡蛋哦。。。)这句话就可以轻松搞定!MappedByteBuffer 只是一种特殊的 ByteB...
关于MappedByteBuffer资源释放问题
JDK1.4中加入了一个新的包:NIO(java.nio.*)。这个库最大的功能(我认为)就是增加了对异步套接字的支持。其实在 其他语言中,包括...
本文主要讲了java中内存映射的原理及过程,与传统IO进行了对比,最后,用实例说明了结果。
最近在写一个txt文本小说阅读器,在解析几M以上的txt文件时,总是由于内存溢出报错,要不然就是运行太慢,在网上找了集中方法后,最后觉得MappedByteBuffer最合适,不仅解决了以上的问题,还...
java的内存映射文件有如下特点:
1,使用虚拟内存,因此分配(map)的内存大小不受JVM的-Xmx参数限制,但是也是有大小限制的,首先他理论上不能超过Integer.MAX_VALUE也就...
最近在看NIO部分,发现基于byteBuffer的MappedbyteBuffer性能比普通的stream在读取文件方面性能强很多。如下:
package com.demo.iodemo.byteb...
他的最新文章
讲师:王哲涵
讲师:王渊命
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)

我要回帖

更多关于 java中put的用法 的文章

 

随机推荐