JavaNIO-文件通道
文件通道,是一种特殊的通道。
对于文件通道来说,是不能阻塞的。所以并不能设置非阻塞模式。
对于异步文件I/O来说,这是很多操作系统支持的,NIO也会增强。
一个FileChannel只能从RandomAccessFile,FileInputStream和FileOutputStream来获取(getChannel),然后你就获得了某种能力,请看:
package java.nio.channels; public abstract class FileChannel extends AbstractChannel implements ByteChannel, GatheringByteChannel, ScatteringByteChannel { // This is a partial API listing// All methods listed here can throw java.io.IOException public abstract int read (ByteBuffer dst, long position) public abstract int write (ByteBuffer src, long position) public abstract long size( )public abstract long position( ) public abstract void position (long newPosition)public abstract void truncate (long size) public abstract void force (boolean metaData) public final FileLock lock( )public abstract FileLock lock (long position, long size, boolean shared) public final FileLock tryLock( ) public abstract FileLock tryLock (long position, long size, boolean shared) public abstract MappedByteBuffer map (MapMode mode, long position, long size) public static class MapMode { public static final MapMode READ_ONLYpublic static final MapMode READ_WRITE public static final MapMode PRIVATE }public abstract long transferTo (long position, long count, WritableByteChannel target) public abstract long transferFrom (ReadableByteChannel src, long position, long count)}
public abstract class FileLock {public final FileChannel channel( )public final long position( ) public final long size( ) public final boolean isShared( ) public final boolean overlaps (long position, long size) public abstract boolean isValid( ); public abstract void release( ) throws IOException; }
FileLock lock = fileChannel.lock( ) try { <perform read/write/whatever on channel> } catch (IOException) [ <handle unexpected exception> } finally {lock.release( )}