简体   繁体   English

尝试获取文件路径以在Java / Netbeans的所有发行版中正常工作

[英]Trying to get file paths to work across all releases in Java/Netbeans

So I'm working on a game, and I need help with my file i/o for savefiles. 因此,我正在开发一个游戏,我需要有关文件I / O的保存文件方面的帮助。 Currently I have something like this setup to read from them: 目前,我有一些类似的设置可以从中读取:

public static void savesManagementMenu() {
    for (int i = 1; i < 4; i++) {
        fileURL = JRPG.class.getResource("Saves/save" + i + ".txt");
        System.out.println(fileURL);
        if (fileURL != null) {
            saveMenuSaveFile = new File(fileURL.getPath());

            try {
                //System.out.println("File # " + i +" exists.");
                saveMenuSaveFileReader = new FileReader(fileURL.getPath());
                saveMenuFileScanner = new Scanner(saveMenuSaveFileReader);

                saveMenuInfo[i - 1][0] = saveMenuFileScanner.nextLine();
                saveMenuFileScanner.nextLine();
                saveMenuFileScanner.nextLine();
                saveMenuFileScanner.nextLine();
                saveMenuInfo[i - 1][1] = saveMenuFileScanner.nextLine();
                saveMenuInfo[i - 1][2] = saveMenuFileScanner.nextLine();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(JRPG.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            saveMenuInfo[i - 1][0] = null;
        }
    }...

And running/compiling using this method from Netbeans will make the application/game look in "E:\\Copy\\JRPG\\build\\classes\\jrpg\\Saves." 使用Netbeans中的此方法运行/编译将使应用程序/游戏在“ E:\\ Copy \\ JRPG \\ build \\ classes \\ jrpg \\ Saves”中显示。

When I clean and build the project, and try to run it via the command line I get a response like this: 当我清理并构建项目后,尝试通过命令行运行它时,将得到如下响应:

jar:file:/C:/Users/Adam/Desktop/New%20folder/JRPG.jar!/jrpg/Saves/save1.txt

Aug 22, 2013 11:54:18 PM jrpg.JRPG savesManagementMenu SEVERE: null java.io.FileNotFoundException: file:\\C:\\Users\\Adam\\Desktop\\New%20folder\\JRPG.jar !\\jrpg\\Saves\\save1.txt (The filename, directory name, or volume label syntax is incorrect) 2013年8月22日晚上11:54:18 jrpg.JRPG savesManagementMenu严重:null java.io.FileNotFoundException:文件:\\ C:\\ Users \\ Adam \\ Desktop \\ New%20folder \\ JRPG.jar!\\ jrpg \\ Saves \\ save1。 txt(文件名,目录名或卷标签语法不正确)

And the game just freezes up. 游戏停滞不前。 The file path for the saves that I want it to look into when I run the compiled code is: C:\\Users\\Adam\\Desktop\\New folder\\Saves 运行编译后的代码时要查看的保存文件路径为:C:\\ Users \\ Adam \\ Desktop \\ New folder \\ Saves

Which would be the relative file path right? 哪个是相对文件路径? How can I fix this problem so that my compiled code looks in the correct location no matter where I run the file from? 我该如何解决此问题,以便无论从何处运行文件,我的编译代码都可以在正确的位置显示? (Lets say my friend wanted to run the game from his computer except he put the "New Folder" folder somwhere other than his desktop) (假设我的朋友想从他的计算机上运行游戏,除了他将“ New Folder”文件夹放置在桌面以外的其他位置)

A embedded resource is not a File and can't be treated as one. 嵌入式资源不是File ,不能视为一个资源。 Also, as far as output files, you shouldn't be trying to save inside your application anyway... 另外,就输出文件而言,无论如何您都不应该尝试将其保存在应用程序中...

Instead, either save the file to the relative location of your application... 而是将文件保存到应用程序的相对位置...

File saveMenuSaveFile = new File("./Saves/save" + i + ".txt");

Or to the users home directory... 或到用户的主目录...

String userHome = System.getProperty("user.home");
File saveMenuSaveFile = new File(userHome + "/.YouApplicationName/Saves/save" + i + ".txt");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM