It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




7年前关闭。





我正在分析FileOutputStream的API。

getChannel()方法可以返回null吗?如果是这样,在什么情况下?

最佳答案

FileOutputStream getChannel()方法代码

 public FileChannel getChannel() {
    synchronized (this) {
        if (channel == null) {
           channel = FileChannelImpl.open(fd, false, true, this, append);

           /*
             * Increment fd's use count. Invoking the channel's close()
            * method will result in decrementing the use count set for
            * the channel.
             */
            fd.incrementAndGetUseCount();
        }
        return channel;
    }
}


调用FileChannelImpl.open()并且此代码始终创建一个新对象

public static FileChannel open(FileDescriptor fd,
                                boolean readable, boolean writable,
                                Object parent, boolean append)
 {
    return new FileChannelImpl(fd, readable, writable, parent, append);
 }

10-06 06:12