Eri*_*luk 1 java jvm scala filelock
我正在处理 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"""))
Run Code Online (Sandbox Code Playgroud)
一切正常。我有一些代码
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))
Run Code Online (Sandbox Code Playgroud)
但是当我尝试删除同一个文件时,我得到
java.nio.file.AccessDeniedException: D:\Users\Eric\Google Drive (New)\Music\Downloaded\Foreigner [Discography HQ]\1977 - Foreigner\03 - Starrider.mp3
Run Code Online (Sandbox Code Playgroud)
我能说的最好的是 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()
}
}
Run Code Online (Sandbox Code Playgroud)
我原以为关闭通道会释放 JVM 需要的任何文件锁。这是我实际打开文件进行读取的唯一代码部分,尽管代码的其他部分确实检查了文件长度,但我不希望 JVM 需要文件锁。
JVM 持有文件锁的其他原因是什么?我怎样才能找到,我怎样才能释放它们?
干杯,埃里克
我只知道 JavaDoc 说什么:
映射一旦建立,就不再依赖于用于创建它的文件通道。特别是关闭通道对映射的有效性没有影响。
和
映射的字节缓冲区及其表示的文件映射在缓冲区本身被垃圾收集之前一直有效。
您可能没有保留缓冲区,但也许它也没有被 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)
}
Run Code Online (Sandbox Code Playgroud)
尝试一下:
$ scalac niolock.scala ; scala niolock.Test
Failure(java.nio.file.AccessDeniedException: D:\tmp\mapped)
Success(())
Run Code Online (Sandbox Code Playgroud)
或者:
如何从在java中使用FileChannel映射的内存中取消映射文件?
| 归档时间: |
|
| 查看次数: |
3177 次 |
| 最近记录: |