Java文件通道保持锁定文件
希望有人能解释我做错了什么。
我有一个创建FileInputStream的DataLoader类。由于FileInputStream实现了Closeable,所以我将该实例创建为try块的一部分。
然后,我将新创建的流传递给DataManager类。这个类打开一个文件通道并将数据读入一个单例类,将所有数据存储到内存块中。由于FileChannel还实现了Closeable,所以我也在try块中实例化它
然后,我从单个线程调用此代码,检查是否有任何文件更改,当发生这种情况时,将创建DataLoader的新实例以重建内存块。但由于文件锁定,此操作经常失败。此代码是运行在windows 10上的Java 1.8标准应用程序的一部分。我是否错误地假设文件通道和文件输入流都关闭了?我在两个类中都添加了调用close方法的代码,但没有成功
任何帮助都将不胜感激。提前谢谢
public class DataManager {
public DataManager(FileInputStream in) throws IOException {
fromInputStream(in);
}
public final void fromInputStream(FileInputStream in) throws IOException {
try (FileChannel ch = in.getChannel()) {
MappedByteBuffer mb = ch.map(MapMode.READ_ONLY, ch.position(), ch.size());
readData(mb); //reads mapped buffer into a byte array, e.g.: mb.get(barray, 0, 1000);
}
}
}
public class DataLoader {
public DataLoader(File binFile) throws FileNotFoundException, IOException {
try (FileInputStream in = new FileInputStream(binFile)) {
DataManager d = new DataManager(in);
} catch (Exception e) {
LOG.error("Something went wrong while loading data.", e);
}
}
}
# 1 楼答案
正如评论中所建议的,这个问题取决于windows对FileChannel的使用有点严格。我用InputStream替换了所有与FileChannel相关的代码,锁定行为消失了