gpt4 book ai didi

java - JVM 在哪里持有我的文件锁?

转载 作者:行者123 更新时间:2023-11-29 03:23:30 26 4
gpt4 key购买 nike

我正在处理 java.nio.file.AccessDeniedException 问题。

我有一个 Scala 程序,如果我这样做:

java.nio.file.Files.delete(FileSystems.getDefault().getPath("""D:\Users\Eric\Google Drive (New)\Music\Downloaded\Foreigner [Discography HQ]\1977 - Foreigner\03 - Starrider.mp3""")) 

一切正常。我这里有一些代码

def delete(path : Path) {
try {
println("deleting " + path)
java.nio.file.Files.delete(path)
} catch {
case exception: Exception => System.err.println(exception)
}
}

val google1 = FileSystems.getDefault().getPath("""D:\Users\Eric\Google Drive\Music\Downloaded\Foreigner [Discography HQ]""")
val google2 = FileSystems.getDefault().getPath("""D:\Users\Eric\Google Drive (New)\Music\Downloaded\Foreigner [Discography HQ]""")

val duplicates = TraversablePaths(List(google1, google2)).duplicateFilesList

println("deleting duplicate files")
duplicates.foreach(_.filter(!_.startsWith(google1)).foreach(delete))

但是当我尝试删除同一个文件时,我得到了

java.nio.file.AccessDeniedException: D:\Users\Eric\Google Drive (New)\Music\Downloaded\Foreigner [Discography HQ]\1977 - Foreigner\03 - Starrider.mp3

我能说的最好的是,JVM 要么锁定了文件,要么锁定了文件所在的目录,但我无法确定位置。检查文件是否相同的代码看起来像

def identical(file1 : Path, file2 : Path) : Boolean = {

require(isRegularFile(file1), file1 + " is not a file")
require(isRegularFile(file2), file2 + " is not a file")

val size1 = size(file1)
val size2 = size(file2)

if (size1 != size2) return false

var position : Long = 0
var length = min(Integer.MAX_VALUE, size1 - position)

val channel1 = FileChannel.open(file1)
val channel2 = FileChannel.open(file2)

try {
while (length > 0) {
val buffer1 = channel1.map(MapMode.READ_ONLY, position, length)
val buffer2 = channel2.map(MapMode.READ_ONLY, position, length)
if (!buffer1.equals(buffer2)) return false
position += length
length = min(Integer.MAX_VALUE, size1 - position)
}
true
} finally {
channel1.close()
channel2.close()
}
}

我原以为关闭 channel 会释放 JVM 需要的任何文件锁。这是代码中我实际打开文件进行读取的唯一部分,尽管代码的其他部分会检查文件长度,但我不希望 JVM 为此需要文件锁。

JVM 持有文件锁还有哪些其他原因?我怎样才能找到并释放它们?

干杯,埃里克

最佳答案

我只知道 JavaDoc 是怎么说的:

A mapping, once established, is not dependent upon the file channel that was used to create it. Closing the channel, in particular, has no effect upon the validity of the mapping.

A mapped byte buffer and the file mapping that it represents remain valid until the buffer itself is garbage-collected.

你可能没有持有缓冲区,但也可能它也没有被 GC。

更新:稍后我会重新启动到 Windows 来尝试一下,但这在 Linux 上不是问题。

更新:...但是在 Windows 上,是的,这就是问题所在。

package niolock

import java.nio.channels._
import java.nio.file._
import FileChannel.MapMode.{ READ_ONLY => RO }

import scala.util._

object Test extends App {
val p = FileSystems.getDefault getPath "D:/tmp/mapped"
val c = FileChannel open p
var b = c map (RO, 0L, 100L)
c.close

Console println Try(Files delete p)
b = null
System.gc()
Console println Try(Files delete p)
}

尝试一下:

$ scalac niolock.scala ; scala niolock.Test
Failure(java.nio.file.AccessDeniedException: D:\tmp\mapped)
Success(())

或者:

Release Java file lock in Windows

How to unmap a file from memory mapped using FileChannel in java?

关于java - JVM 在哪里持有我的文件锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22335712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com