*Character ---scatter/gather ---always blocking ---attempts to use native I/O services when possible...
*Character
---scatter/gather
---always blocking
---attempts to use native I/O services when possible
---ThreadSafe,but Operations that affect the channel's position or the file size are single-threaded
*API
---truncate(long size)
if size<CurrentSize then discarding any bytes beyond the new end of the file
if size>=CurrentSize then the file is not modified.
if position>size ,then position=size
---force(boolean metaData)
将缓存内容同步到磁盘
metaData的含义,true表示将file的meta更新信息也保存到文件,false表示只更新文件的内容.
这个方法将引起可能的IO操作.
---FileChannel.position(long newPosition)
设置读取或修改的位置,注意FileChannel的position和文件流的position是同步的.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
FileChannel测试/**
* Mar 11, 2011 by dzh
*/
package nio.channel;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
/**
* @author dzh
*
*/
public class FileChannelTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// InputStream is =FileChannelTest.class.getResourceAsStream("file.txt");
if(!new File("file.txt").exists()){ //内容:123abc兵者,诡道也.故能而示之不能,用而示之不用//编码utf-8
System.out.println("Not found!");
return;
}
RandomAccessFile raf = new RandomAccessFile(new File("file.txt"), "rw");
FileChannel fc =raf.getChannel();
System.out.println(raf.length()); //63
//first position
raf.seek(1);
System.out.println(fc.position()); //1
printCurrentContent(raf); //23abc兵
//second
fc.position(8);
System.out.println(raf.getFilePointer()); //8
printCurrentContent(raf); //�者,诡
//truncate
fc.truncate(6);
System.out.println(fc.size()); //6
System.out.println(fc.position()); //16 why?
System.out.println(raf.getFilePointer()); //16
fc.force(true);
printFileContent(raf); //123abc
fc.close();
raf.close();
}
private static void printCurrentContent(RandomAccessFile raf){
byte[] b =new byte[8];
try {
raf.read(b);
System.out.println(new String(b,"utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
private static void printFileContent(RandomAccessFile raf){
try {
raf.seek(0);
byte[] buf =new byte[32];
int len =-1;
ByteArrayOutputStream bos =new ByteArrayOutputStream();
while((len=raf.read(buf))!=-1){
bos.write(buf, 0, len);
}
System.out.println(bos.toString("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
*文件锁FileLock
---用于进程间并发
Locks are associated with files, not channels. Use locks to coordinate
with external processes, not between threads in the same JVM.
---概念
共享锁:共享读操作,但只能一个写
排它锁:读写一时一个.
---FileLock FileChannel.lock(long position, long size, boolean shared) ,文件锁
shared的含义:是否使用共享锁,一些不支持共享锁的操作系统,将自动将共享锁改成排它锁.
---lock()和tryLock()的区别
lock()阻塞的方法
tryLock()非阻塞,当未获得锁时,返回null.
---FileLock的生命周期
在调用FileLock.release(),或者Channel.close(),或者JVM关闭
---FileLock是线程安全的
---boolean java.nio.channels.FileLock.overlaps(long position, long size)
true表示当前锁在区域内,false表示当前锁的区域与参数区域不重叠