FileChannel带缓冲

//1. read the point location
FileChannel channelRead = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.READ);

ByteBuffer buffer = ByteBuffer.allocate(1024);
channelRead.read(buffer);

buffer.flip();    //flip()方法将界限设置到当前位置,位置复位为0,为读入做好准备
while(buffer.hasRemaining()){
    System.out.print((char)buffer.get());
} 
System.out.println();
channelRead.close();


//重置为空状态;它并不改变缓冲区中的任何数据元素,而是仅仅将 limit 设为容量的值,并把 position 设回 0
buffer.clear();


//2. write the point location
FileChannel channelWrite = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.WRITE);

//指定容量的缓冲区
//        buffer = ByteBuffer.allocate(1024);
//        buffer.put("测似乎".getBytes());

//对给定数组的缓冲区
buffer = ByteBuffer.wrap("abcdefg  Java开发手册".getBytes());
buffer.limit("abcdefg  Java开发手册".getBytes().length);        //Sets this buffer's limit

while(buffer.hasRemaining()){
    channelWrite.write(buffer);
} 
channelWrite.close();

ByteBuffer

//1. ByteBuffer
//(1) 读取buffer
ByteBuffer byteBuffer = ByteBuffer.wrap("111 222 333 444 555 666 777".getBytes());

//		byteBuffer.flip();	//flip()方法将界限设置到当前位置,位置复位为0,为读入做好准备
while(byteBuffer.hasRemaining()){
    System.out.print((char)byteBuffer.get());
} 
System.out.println();

//重新读取缓冲区
byteBuffer.rewind();
while(byteBuffer.hasRemaining()){
    System.out.print((char)byteBuffer.get());
} 
System.out.println();


byteBuffer.rewind();

byteBuffer.position(12);	//返回缓冲区的位置
byteBuffer.compact();		//读取position指向的位置和limit直接的值; 丢弃已经释放的数据,保留未释放的数据,并使缓冲区对重新填充容量准备就绪
while(byteBuffer.hasRemaining()){
    System.out.print((char)byteBuffer.get());
} 
System.out.println();


//(2) 写buffer
byteBuffer = ByteBuffer.wrap("abcdefg  Java开发手册".getBytes());
byteBuffer.limit("abcdefg  Java开发手册".getBytes().length);		//Sets this buffer's limit

while(byteBuffer.hasRemaining()){
    System.out.println(byteBuffer.get());
} 


//(3) 转换
byteBuffer.clear();
CharBuffer asCharBuffer = byteBuffer.asCharBuffer();
ByteBuffer asReadOnlyBuffer = byteBuffer.asReadOnlyBuffer();

System.out.println();



CharBuffer

//(1) 读
CharBuffer charBuffer = CharBuffer.wrap("abcdefg  Java开发手册".toCharArray());

System.out.println(charBuffer.position());
System.out.println(charBuffer.limit());
System.out.println(charBuffer.toString());		//返回包含此缓冲区中字符的字符串


charBuffer.append("第一期面授培训大纲", charBuffer.position(), charBuffer.position()+"第一期面授培训大纲".length());
System.out.println(charBuffer.position());
System.out.println(charBuffer.limit());
int limit = charBuffer.limit();

charBuffer.flip();
charBuffer.limit(limit);

System.out.println(charBuffer.toString());
System.out.println(charBuffer.position());
System.out.println(charBuffer.limit());

//(2) 写


//(3) 转换
charBuffer.clear();
CharBuffer asReadOnlyBuffer2 = charBuffer.asReadOnlyBuffer();

Channel lock/tryLock

//3. 锁机制
//1). 对于一个只读文件通过任意方式加锁时会报NonWritableChannelException异常  
//2). 无参lock()默认为独占锁,不会报NonReadableChannelException异常,因为独占就是为了写  
//3). 有参lock()为共享锁,所谓的共享也只能读共享,写是独占的,共享锁控制的代码只能是读操作,当有写冲突时会报NonWritableChannelException异常  

//(1) 锁定文件, 默认为独占锁, 阻塞的方法, 当文件锁不可用时,当前进程会被挂起
try(FileChannel channel = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.WRITE);
		FileLock lock = channel.lock();
		){
	ByteBuffer sendBuffer=ByteBuffer.wrap((new Date()+" 写入\n").getBytes());  
	channel.write(sendBuffer);
	lock.release();
}

//(2) 锁定文件, 默认为独占锁, 非阻塞的方法, 当文件锁不可用时,tryLock()会得到null值  
try(FileChannel channel = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.WRITE);){
	FileLock lock = null;
	try{
		do{
			lock = channel.tryLock();
		}while(null==lock);
    	
    	ByteBuffer sendBuffer=ByteBuffer.wrap((new Date()+" 写入\n").getBytes());  
    	channel.write(sendBuffer);
    	lock.release();
	}finally {
		if(null!=lock){
			lock.close();
		}
	}
}

//release


//(3) 锁定部分文件
//shared = true, 表示为共享锁, 多个进程可以读入, 阻止其它获得独占的锁
try(FileChannel channel = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.WRITE);
		FileLock lock = channel.lock(2, 3, false);
		){
	ByteBuffer sendBuffer=ByteBuffer.wrap((new Date()+" 写入\n").getBytes());  
	channel.write(sendBuffer);
}

//(4) 锁定部分文件
try(FileChannel channel = FileChannel.open(Paths.get("E:\\888.txt"), StandardOpenOption.WRITE);){
	FileLock lock = null;
	try{
		do{
			lock = channel.tryLock(2, 3, false);
		}while(null==lock);
    	
    	ByteBuffer sendBuffer=ByteBuffer.wrap((new Date()+" 写入\n").getBytes());  
    	channel.write(sendBuffer);
	}finally {
		if(null!=lock){
			lock.close();
		}
	}
}






posted on 2018-02-08 23:27  刘达人186  阅读(114)  评论(0编辑  收藏  举报