我需要读取和处理大文件Java和我想知道,如果有一些明智的方式保护文件不会被复盖的其他进程的同时,我阅读和处理它?

这是,某种方式使它只读,保持它的"开放"或东西...

这将在Windows环境。

br, 的样子

有帮助吗?

解决方案

你想要一个 文件锁:

FileChannel channel = new RandomAccessFile("C:\\foo", "rw").getChannel();

// Try acquiring the lock without blocking. This method returns
// null or throws an exception if the file is already locked.
FileLock lock = channel.tryLock();

// ...  

// release it
lock.release();

为了简单起见,我省略了一个enclosng试用/最终块,但你不应该在你的生产码

其他提示

工作主要是好的,得到了一些问题: 作为锁定似乎不能完全绝对.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top