0

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

  • 1
    Depended on the platform, this isn't entirely possible. Think for second. Other then changing the file permissions, which is a lot of hassle on Windows, if you make the file read only, then you can't (technically) write to it. You'd be better of writing the file to somewhere the user is unlikely to go messy around – MadProgrammer May 14 '13 at 06:50
  • 1
    You could simply run your application with a different user and set destination folder permissions so that other users cannot write in it – Maxx May 14 '13 at 06:53

1 Answers1

2

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
Community
  • 1
  • 1
AlexR
  • 114,158
  • 16
  • 130
  • 208