FileLock 文件锁
程序员文章站
2022-06-04 11:32:06
...
File file = new File("test.txt");
RandomAccessFile rw = null;
FileLock fileLock = null;
try {
rw = new RandomAccessFile(file, "rw");
// 无参数,为独占锁
fileLock = rw.getChannel().lock();
String line ="add";
rw.seek(rw.length());
rw.write(System.lineSeparator());
rw.write(line.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fileLock !=null) fileLock.release();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (rw !=null) rw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
上一篇: SpringBoot(1)
下一篇: JavaSE之——并没有多维数组