0

I have looked to find an answer for this question, but I couldn't find anything that had exactly what I wanted, so I decided to ask here!

I am making a Java game. I am trying to save information locally to the game user, in something like a folder in their Application Support or Documents or whatever (kind of like Minecraft). For example, I am trying to save information like where they left off, and if this is their first time playing, and all that stuff. I was wondering how to create a folder somewhere on that person's computer, and then save a file in it. Is it different for PC and Mac users? How would I go about doing this? How would I save the file on the user's computer?

Also, what is a good file to read/write to save game data? I've used YAML before (I am a Minecraft Bukkit Developer), but I know about XML (I haven't used it before, though). So I just wanted to know, on top of the question above about saving, what file to USE AND HOW for Java.

Please help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Blockhead7360
  • 145
  • 10

2 Answers2

0

How to create a folder using Java: How to create empty folder in java?

Scanner, a class which can read IO from files: https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Scanner.html

As for the format you want to save the file as - Whatever you're comfortable with. It sounds to me like you're working with a key:value structure of data, so why don't you took a look at Java's properies files: http://www.avajava.com/tutorials/lessons/how-do-i-read-a-properties-file.html

Magical Gordon
  • 404
  • 2
  • 8
  • Thanks! But how do I get the player's computer? Like in the path name, where does it start? Ex: File file = new File("/something/net/red.properties"). Where is "something"?!? – Blockhead7360 Jan 09 '16 at 04:56
  • iirc, "something" is the same directory as wherever the program is being run from. So if you have a folder with minecraft.jar, whatever folder contanis that .jar file, is "root" (the first /) – Magical Gordon Jan 09 '16 at 05:09
  • Have a look at the user.home system property – David Soroko Jan 10 '16 at 09:24
0

The method of saving data is quiet simple. Take this printwriter example.

    File outFile = new File ("output.txt");
    FileWriter fWriter = new FileWriter (outFile);
    PrintWriter pWriter = new PrintWriter (fWriter);
    pWriter.println ("10, 10"); //x and y
    pWriter.println ("100"); //health
    pWriter.close();

All you really need to do is save the information in some sort of a file for later use, here, i used a normal tecxt file.

Then you use a Scanner to read in your data you saved ans assign it to your variables.

File inFile = new File ("input.txt");

Scanner sc = new Scanner (inFile);
while (sc.hasNextLine())
{
  String line = sc.nextLine();
  System.out.println (line); //assign variables here 
}
sc.close();

Although this is a working way of saving data, it can be very easy to tamper with. A normal player would be able to edit and change his x,y coords, health or what ever your storing in it. You can use forms of Cryptography or other ways to keep your user from tampering with his data saves.

Ryan
  • 1,972
  • 2
  • 23
  • 36