0

I am writing a game in java, and i would like to save my game. I am going to save it as a set of XML documents in a zip file. What i would like to know is if their is a way i can do this transparently so that the user cannot edit these files, or find them. I have thought about just making the files hidden, but that seems like a dirty and ugly way to do this. The thing i like least about the hidden files is that they don't seem compatible between systems. If their was anyway to save the files to the jar and read them from it that would also be equally as acceptable. Also there doesn't seem like a good way in java to make a file hidden. Any help would be greatly appreciated.

Josh Sobel
  • 1,268
  • 3
  • 14
  • 27
  • What about using object serialization? The result will be hard to edit, but not impossible. – John Tang Boyland Apr 29 '13 at 22:31
  • i know but then i have to create a saves folder, and i will also have to worry about compatibility between versions, i am writing more than one object at once additionally, however i am using xml to serialize my objects using XMLEncoder and saving all the xml-files to a zip. – Josh Sobel Apr 29 '13 at 22:33
  • You could use your XML format but put it through a salted hash, just so players couldn't directly edit it without potentially corrupting it. – jeff Apr 29 '13 at 22:57

1 Answers1

0

What i would like to know is if their is a way i can do this transparently so that the user cannot edit these files, or find them.

Ultimately, no.

Anything that you do to store state on the local machine can ultimately be reverse engineered and that state can be retrieved by a privileged user.

But there are various things you could do to make it difficult for people to "cheat". For instance, you could encrypt the file, or generate a seeded hash to detect "tinkering".


Hiding files on Java 7 is simple:

Path path = FileSystems.getDefault().getPath("directory", "hidden.txt");
Boolean hidden = path.getAttribute("dos:hidden", LinkOption.NOFOLLOW_LINKS);
if (hidden != null && !hidden) {
    path.setAttribute("dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
}

(Copied from: http://javarevisited.blogspot.com/2012/01/how-to-hide-file-java-program-example.html)

On earlier versions of Java you needed to call an external Windows application or call a native library to do it. But note that:

  • "hidden" files are only hidden from users who don't bother to look
  • they work differently on different platforms; e.g. for Linux/Unix based systems, "hidden" just means that the filename starts with a ".".
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216