2012-08-15 74 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()" 
... 

,我发现这个问题很令人沮丧,当我遇到它,所以我想我会与其他可能需要它的人分享我的解决方案。