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()
}
}
チャネルを閉じると、JVM が必要とするファイル ロックが解放されると思っていたでしょう。これは、実際にファイルを読み取り用に開くコードの唯一の部分です。コードの他の部分はファイルの長さをチェックしますが、JVM がそのためにファイル ロックを必要とすることはないと思います。
JVM がファイル ロックを保持している他の理由は何ですか? どうすれば見つけられますか? また、どうすればそれらを解放できますか?
乾杯、エリック