java-NIO-RandomAccessFile
RandomAccessFile
只能访问文件,不能操作其他io设备
支持随机访问
在读写等长记录文件有优势
使用例子:
    RandomAccessFile randFile = new RandomAccessFile(filename, "rw");
    FileChannel inChannel = randFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(48);
    int bytesRead = inChannel.read(buf); //read into buffer.
    while (bytesRead != -1) {
        buf.flip();  //make buffer ready for read
        while(buf.hasRemaining()){
            System.out.print((char) buf.get()); // read 1 byte at a time
        }
        buf.clear(); //make buffer ready for writing
        bytesRead = inChannel.read(buf);
    }
    randFile.close();
属性
    private FileDescriptor fd;
    private FileChannel channel = null;
    private boolean rw;
    private final String path;
    private Object closeLock = new Object();
    private volatile boolean closed = false;
    private static final int O_RDONLY = 1;
    private static final int O_RDWR =   2;
    private static final int O_SYNC =   4;	//同步
    private static final int O_DSYNC =  8;	//异步
构造
输入文件名和读写模式,如果文件存在创建File(name)
 public RandomAccessFile(String name, String mode)
        throws FileNotFoundException {
        this(name != null ? new File(name) : null, mode);
    }
配置文件的读写模式,检测权限,创建文件描述符,把这个File和文件描述符绑定,再以配置的模式打开文件
    public RandomAccessFile(File file, String mode)
        throws FileNotFoundException {
		//name是全路径名
        String name = (file != null ? file.getPath() : null);
        int imode = -1;
        if (mode.equals("r"))
            imode = O_RDONLY;
        else if (mode.startsWith("rw")) {
            imode = O_RDWR;
            rw = true;
            if (mode.length() > 2) {
                if (mode.equals("rws"))
                    imode |= O_SYNC;
                else if (mode.equals("rwd"))
                    imode |= O_DSYNC;
                else
                    imode = -1;
            }
        }
        if (imode < 0)
            throw new IllegalArgumentException("Illegal mode \"" + mode
                                               + "\" must be one of "
                                               + "\"r\", \"rw\", \"rws\","
                                               + " or \"rwd\"");
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkRead(name);
            if (rw) {
                security.checkWrite(name);
            }
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        fd = new FileDescriptor();
        fd.attach(this);
        path = name;
        open(name, imode);
    }
创建FileDescriptor时,初始化
	fd = -1;
	handle = -1;
	initIDs();	//配置JNI
initIDs方法在SharedSecrets类中设置了一个JavaIOFileDescriptorAccess
在FileDescriptor中attach方法,加入的都是可关闭的类,主要是把parent设置为这个File
 	synchronized void attach(Closeable c) {
        if (parent == null) {
            parent = c;
        } else if (otherParents == null) {
            otherParents = new ArrayList<>();
            otherParents.add(parent);
            otherParents.add(c);
        } else {
            otherParents.add(c);
        }
    }
getChannel
	public final FileChannel getChannel() {
        synchronized (this) {
            if (channel == null) {
                channel = FileChannelImpl.open(fd, path, true, rw, this);
            }
            return channel;
        }
    }
FileChannelImpl的open方法构建了一个FileChannelImpl,并做了如下的初始化
    private FileChannelImpl(FileDescriptor fd, boolean readable,
                            boolean writable, boolean append, Object parent) {
        this.fd = fd;
        this.readable = readable;
        this.writable = writable;
        this.append = append;
        this.parent = parent;
        this.nd = new FileDispatcherImpl(append);
    }
把当前RandomAccessFile中的文件描述符和读写等特殊性传入,并打开了一个FileChannelImpl
这样就打开channel了
 
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号