-1

I'm creating a program in Java that needs to create a file that will store a list of files accessible for various users. However I want to lock the file such that you couldn't just go to the file path outside of the program and view/change the file. Is there a way to do this?

Edit: I was looking into FileLock, but I couldn't tell if that locked the file just while the program was running, or if it locked it all times for anything trying to access it.

Eric M
  • 57
  • 1
  • 8
  • 1
    http://stackoverflow.com/a/128168/2798955 – Bruce Jul 27 '15 at 19:17
  • 1
    possible duplicate of [How can I lock a file using java (if possible)](http://stackoverflow.com/questions/128038/how-can-i-lock-a-file-using-java-if-possible) – Blake Yarbrough Jul 27 '15 at 19:17
  • Exbury, I read that before. It seems like that solution only locks the file while the program is open. I need the file locked all the time, even when the program isn't running. – Eric M Jul 27 '15 at 19:19
  • It's a matter of permissions, not of locking. What's your operating system? – RealSkeptic Jul 27 '15 at 19:21
  • I want to be able to do it in Linux and Windows. I'm making the program in linux, but it will be run in windows. I know it's not ideal, but it's what I'm stuck with. – Eric M Jul 27 '15 at 19:23

1 Answers1

0

I think you are going the wrong way : you need the OS to make a file read-only and everybody with enough privileges will be able to override this and modify the file.

Instead of locking the file just encrypt it. It would be much more appropriate for your needs. See https://stackoverflow.com/a/27962481/1980659 for easy file encryption in Java.

Community
  • 1
  • 1
ForguesR
  • 3,558
  • 1
  • 17
  • 39