Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Java Multi Platform Save Directory

Okay so I'm currently creating a game and I've having a problem with the save feature. Currently I use WindowsOS and the Windows side of the code works a treat, but since I don't have other OS systems like mac or linux I don't know if this code would work on all platforms (the major ones)

Here the code.

Spoiler (Click to View)

So, what do people suggest or what is the right way for obtaining like an AppData folder for other platforms like Mac and Linux?

I would also appreciate replies from Mac & Linux users too

Thanks
For mac use ~/Library/Application Support
... Not sure what people usually use for most distros of Linux, but somewhere in the home folder works.

ninja: I don't think you can use backslashes for anything other than dos. File.separator or just "/" works.
So, for a mac and linux user.

Will

Code
fullDirectory = Paths.get(System.getProperty("user.home") + "\\Library\\Application Support\\NoughtPlusPlus");
      fullPath = Paths.get(fullDirectory.toString() + "\\Library\\Application Support\\RecentScores.npp");

Work?

Also, seems you're right about the backslash thing, thanks :D
I know the Minecraft Launcher uses the following method:
Code
public static final String APPLICATION_NAME = "minecraft";

    public enum OS
    {
      WINDOWS,  MACOS,  SOLARIS,  LINUX,  UNKNOWN;
    }

    public static OS getPlatform()
    {
      String osName = System.getProperty("os.name").toLowerCase();
      if (osName.contains("win")) {
      return OS.WINDOWS;
      }
      if (osName.contains("mac")) {
      return OS.MACOS;
      }
      if (osName.contains("linux")) {
      return OS.LINUX;
      }
      if (osName.contains("unix")) {
      return OS.LINUX;
      }
      return OS.UNKNOWN;
    }

    public static File getWorkingDirectory()
    {
      String userHome = System.getProperty("user.home", ".");
      File workingDirectory;
      switch (getPlatform().ordinal())
      {
      case 1:
      case 2:
            workingDirectory = new File(userHome, "." + APPLICATION_NAME + "/");
            break;
      case 3:
            String applicationData = System.getenv("APPDATA");
            String folder = applicationData != null ? applicationData : userHome;

            workingDirectory = new File(folder, "." + APPLICATION_NAME + "/");
            break;
      case 4:
            workingDirectory = new File(userHome, "Library/Application Support/" + APPLICATION_NAME);
            break;
      default:
            workingDirectory = new File(userHome, APPLICATION_NAME + "/");
      }
      return workingDirectory;
    }
(Jun 25, 2015 07:42 PM)JershDervis Wrote:  I know the Minecraft Launcher uses the following method:
Code
public static final String APPLICATION_NAME = "minecraft";

    public enum OS
    {
      WINDOWS,  MACOS,  SOLARIS,  LINUX,  UNKNOWN;
    }

    public static OS getPlatform()
    {
      String osName = System.getProperty("os.name").toLowerCase();
      if (osName.contains("win")) {
      return OS.WINDOWS;
      }
      if (osName.contains("mac")) {
      return OS.MACOS;
      }
      if (osName.contains("linux")) {
      return OS.LINUX;
      }
      if (osName.contains("unix")) {
      return OS.LINUX;
      }
      return OS.UNKNOWN;
    }

    public static File getWorkingDirectory()
    {
      String userHome = System.getProperty("user.home", ".");
      File workingDirectory;
      switch (getPlatform().ordinal())
      {
      case 1:
      case 2:
      workingDirectory = new File(userHome, "." + APPLICATION_NAME + "/");
      break;
      case 3:
      String applicationData = System.getenv("APPDATA");
      String folder = applicationData != null ? applicationData : userHome;

      workingDirectory = new File(folder, "." + APPLICATION_NAME + "/");
      break;
      case 4:
      workingDirectory = new File(userHome, "Library/Application Support/" + APPLICATION_NAME);
      break;
      default:
      workingDirectory = new File(userHome, APPLICATION_NAME + "/");
      }
      return workingDirectory;
    }

That's decompiled from the launcher, right? I'm just assuming that because enum switches use ordinal() after being compiled and no one writes enum switches with ordinal
If you truly want it to be multi-platform, save yourself the trouble and 1) allow the user to choose their own directory and 2) store all save profiles and settings files in a sub-directory. Don't worry about how Mojang does it. You can always make it more sophisticated later.
(Jun 26, 2015 04:51 AM)DragonBook Wrote:  If you truly want it to be multi-platform, save yourself the trouble and 1) allow the user to choose their own directory and 2) store all save profiles and settings files in a sub-directory. Don't worry about how Mojang does it. You can always make it more sophisticated later.

What sub directory should I save it to?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by -DarkByDesign-May 14, 2020 12:13 PM 0 124 May 14, 2020 12:13 PM
Last Post: -DarkByDesign-
[Java] by DessieOct 4, 2016 09:56 AM 5 169 Oct 4, 2016 09:56 AM
Last Post: Dessie
[C#] by gotoelAug 15, 2016 01:35 PM 15 1,066 Aug 15, 2016 01:35 PM
Last Post: Ninja