我们可以打开一个简单的频道
Files.newByteChannel(path)
那就行了。问题是,如果我要打开多个频道,则必须打开一个:
Channel chan1 = Files.newByteChannel(path);
Channel chan2 = Files.newByteChannel(path);
一般来说,上面的例子并不适用。考虑一下情况:
Channel chan1 = Files.newByteChannel(path); //OK
//Some other process moved path and created an new file with the path
Channel chan2 = Files.newByteChannel(path); //Damn!
比赛条件出现。在Linux中,我们有
dup
-像系统调用一样
fcntl(F_DUPFD, int)
int fd == open(path);
int duplicated = fcntl(fd, F_DUPFD, fd);
这应该管用。
有没有办法在Java中做到这一点,避免写
JNI
功能?
upd:我要复制的原因是我想将数据从一个文件传输到多个文件
SocketChannel
同时。所以有一个单独的
FileChannel
转单程票
插座通道
.