【发布时间】:2014-08-11 07:42:37
【问题描述】:
这是我正在运行的代码:
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Main {
public static void main(String[] args) throws Exception {
String filePath = "D:/temp/file";
RandomAccessFile file = new RandomAccessFile(filePath, "rw");
try {
MappedByteBuffer buffer = file.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 128);
// Do something
buffer.putInt(4);
} finally {
file.close();
System.out.println("File closed");
}
System.out.println("Press any key...");
System.in.read();
System.out.println("Finished");
}
}
在按键之前,我尝试在 FAR 管理器中手动删除文件。但 FAR 说文件被锁定:
The process cannot access the file because it is being used by another process.
Cannot delete the file
D:\temp\file
Object is being opened in:
Java(TM) Platform SE binary (PID: 5768, C:\Program Files\Java\jdk1.8.0_05\bin\javaw.exe)
只有在按键后,应用程序才会终止,我可以删除文件。
我的代码有什么问题?
【问题讨论】:
-
似乎对我有用。您是否尝试过查找任何正在运行的 Java 进程并通过在 Windows 上按 shift+escape 来关闭它们/关闭您可能打开并再次运行它的所有文件实例。
-
没有帮助。我可以选择任意文件(保证是新的),例如D:/temp/file2441,仍然面临同样的问题。
标签: java java-9 randomaccessfile mappedbytebuffer