0
我使用Java中的FileLock锁定了文件,但现在无法读取或写入文件。我该怎么办?无法读取或写入Java中的锁定文件
我使用Java中的FileLock锁定了文件,但现在无法读取或写入文件。我该怎么办?无法读取或写入Java中的锁定文件
虽然可能有这个问题,很多潜在的解决方案,我发现了以下工作相当不错:
// Gets a readable and writable channel to your file.
FileChannel channel = new RandomAccessFile(yourFile, "rw").getChannel();
// Allows you to read from the file.
InputStream in = Channels.getInputStream(channel);
// Allows you to write to the file.
OutputStream out = Channels.getOutputStream(channel);
// Lock the file here as you see fit to prevent concurrency issues.
// As a concrete example, you could attempt to lock the file using "channel.tryLock()"
...
,我发现这个问题很令人沮丧,当我遇到它,所以我想我会与其他可能需要它的人分享我的解决方案。
定义“不能”。会发生什么呢? – EJP 2012-08-15 22:48:14