How do I create directories and files within the user's AppData directory?

醉酒当歌 提交于 2019-12-22 01:17:14

问题


So, I'm creating a game and need to create directories to store all the user's data and saves and whatnot. As you can see, I need to create a folder in the Application Data folder of the user called, "[WarDungeon]", and there, I will store other folders for such data like levels, the bin, sprites, etc.

I am not too interested in working with Mac OS and Linux as I want to get the %appdata% folder working with Windows to begin with.

Here's my code:

public FileManage() {
    gamePath  = System.getenv("APPDATA") + "[WarDungeon]";
    gameLevelPath  = System.getProperty("user.home") + "\\Local Settings\\ApplicationData\\[WarDungeon]\\level";
    gameBinPath  = System.getProperty("user.home") + "\\Local Settings\\ApplicationData\\[WarDungeon]\\bin";

    File createDir1 = new File(gamePath);
    createDir1.mkdir();

    System.out.println("First test passed.");

    if (createDir1.exists() == true) {
        System.out.println("First directory created!");
    }
}

How do I fix this?

Thanks in advance, :)


回答1:


Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)



回答2:


I realize that this question is two years old, but since this is the first result while googling "java store in appdata", I decided to add a correct answer for future googlers.

The question is tagged with Java, and the OP posted Java code, but the accepted answer is .NET (not Java).

Quoted directly from this answer:

System.getenv("APPDATA")

This will give the location to the user's AppData folder (in Windows). You can get a result similar to what the OP wanted by doing the following:

File characterFolder = new File(System.getenv("APPDATA") + "\\" + characterName);


来源:https://stackoverflow.com/questions/18906163/how-do-i-create-directories-and-files-within-the-users-appdata-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!