0

Java で FileLock を使用してファイルをロックしましたが、読み書きができなくなりました。私は何をしますか?

4

1 に答える 1

2

この問題には多くの潜在的な解決策があるかもしれませんが、次の方法が非常にうまく機能することがわかりました。

// 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()"
...

この問題に遭遇したとき、私は非常に苛立たしいことに気付いたので、私の解決策を必要とする他の人と共有したいと思いました.

于 2012-08-15T21:52:05.830 に答える