Reputation: 1
I am have a requirement in which an excel is generated in my c: folder that is through Apache POI and I want if some user forcefully try to delete it by going to that location that is by selecting with mouse and hitting the delete key then that particular excel is not need to be get deleted , I want to make it protected one , Please let me know how through java code I can make this excel file not to be deleted one, any setting in apache poi
Upvotes: 0
Views: 859
Reputation: 115328
Take a look on this discussion: How can I lock a file using java (if possible)
Shortly speaking use channel lock like the following:
FileLock lock = new FileInputStream(paht).getChannel().lock();
try {
// do what you need
} finally {
lock.release();
}
or even better using the new feature of java 7:
try (
FileLock lock = new FileInputStream(paht).getChannel().lock();
) {
// do what you need
}
// file lock is AutoClosable, so there is no need to call its release() explicitly
Upvotes: 2