2012-08-15 75 views

回答

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

,我發現這個問題很令人沮喪,當我遇到它,所以我想我會與其他可能需要它的人分享我的解決方案。