2

I have a program that temporarily saves some Objects to my Hard-Disk only to be re-loaded a few minutes after:

This is the method:

private void save(Object myObject, String path) throws IOException {
        final BufferedOutputStream bf = new BufferedOutputStream(
                new FileOutputStream(path));

        final ObjectOutputStream objOut = new ObjectOutputStream(bf);

        objOut.writeUnshared(myObject);

        bf.close();
        bf.flush();
        objOut.flush();
        objOut.reset();
    }

The problem is that I'm able to delete the file from the disk at runtime, and ending up in a NoSuchFileException later: How to prevent this from happening?

I also tried using

objOut.writeObject(myObject);

instead of

objOut.writeUnshared(myObject);

but still nothing...

Can someone help me? Thanks!

EDIT Read comments for a more detailed description of the request!

Dea5
  • 131
  • 1
  • 9
  • You can implement a file lock. Have a look [here](http://stackoverflow.com/questions/128038/how-can-i-lock-a-file-using-java-if-possible) . – Abhishek Pathak May 05 '17 at 10:36
  • Sounds like this could be an [XY-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why are you serializing objects to a file system when the same application is reloading them anyways? Why are you assuming that users will delete those files in that short time they exist? – André Stannek May 05 '17 at 10:40
  • @AbhishekPathak from java doc of FileLock: Whether or not a lock actually prevents another program from accessing the content of the locked region is system-dependent and therefore unspecified. The native file-locking facilities of some systems are merely advisory, meaning that programs must cooperatively observe a known locking protocol in order to guarantee data integrity – Davide Lorenzo MARINO May 05 '17 at 10:41
  • @AndréStannek I'm writing a program that has to generate billions of integers, so I temporarily store them in a lot of arrays, save them, and when needed, the arrays are reloaded: maybe i did not express myself well, I **DO NOT CLOSE** the application between the save and reload of the arrays, I just want a temporarily bind between the files and my program that disappear when the application is closed, then someone **CAN DELETE** the files if he wants. – Dea5 May 05 '17 at 11:03
  • 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) – Matthias May 05 '17 at 11:55
  • @Matthias Thanks Matthias, but I don't know if this is what I am looking for, because I don't know how to use it... – Dea5 May 05 '17 at 12:20
  • Sorry, i did not look carefully... – Matthias May 05 '17 at 12:26

1 Answers1

0

Basically you can't but you can mitigate this problem as follow:

  • create a specific system user to execute your java application
  • create a folder visible only to that system user
  • launch your java app with that user

This mitigate the problem because a human can always log a the system user defined before so having full control over the private files.

You can also hide the directory where you save the files to make more hard for a human to delete them.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56