请求并发文件锁处理

//加锁
String fileName = ouId + "-back";
File file = new File("C:/temp/" + fileName);
if (!file.exists()) {
file.createNewFile();
}
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
fileChannel = randomAccessFile.getChannel();
lock = fileChannel.lock();

finally {
if (lock != null) {
try {
lock.release();
lock = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileChannel != null) {
try {
fileChannel.close();
fileChannel = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}

猜你喜欢

转载自www.cnblogs.com/xingchi/p/9579498.html