相对路径 Java

Relative Path Java

我在 Java 中遇到了一些问题。这是一个从 txt 文件中读取关卡定义的程序。它还读取图像。它的工作原理使我有一个“资源”文件夹(在程序文件夹内)并且在其中包含我需要的所有 folders/files 。这是资源文件夹的示例:

资源

background_images

clouds.png

jungle.jpg

night.jpg

block_images

leopard.jp

zebra.jpg

定义

level_definitions.txt

level_definitions.txt里面,还有各种你应该重定向到的其他文件名,比如

background_images/night.jpg  or  definitions/moon_block_definitions.txt

这就是程序,我能够编写代码,它工作得很好。

虽然在作业结束时,他们添加了这条注释:

A note on file locations

All the file names specified in the levels and block definition files should be relative to the class path. The reason we want them to be relative to the class path is that later we will be able to read the files from inside a jar, something we can not do with regular File references.

To get an input stream relative to the class path (even if it's inside a jar), use the following:

InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("image.png");
The idea is to keep a folder with files(definitions and images) and then add that folder to the class path when running the JVM:

java -cp bin:resources ... 
If you don't add the resources folder to you class path you wont be able to load them with the command from above.

我真的不知道他们这是什么意思,我应该如何更改我的代码。 (我的教授没有回答,作业将在几天内到期)。

这是我的代码现在读取文件的方式:

这是我阅读主文件的方式:

java.io.Reader reader = null;
try {
    reader = new InputStreamReader(new FileInputStream("resources/definitions/level_definitions.txt"));
} catch (FileNotFoundException e) {
    e.printStackTrace();
} 
is = new BufferedReader(reader);

这是我阅读其他文件的方式:

// blocks txt
reader = new InputStreamReader(new FileInputStream("resources/" + stringPath)); 
is = new BufferedReader(reader);

// images
File pathToFile = new File("resources/" + stringPath);
try {
   backgroundImage = ImageIO.read(pathToFile);
} catch (IOException e) {
   e.printStackTrace();
}

现在,我试过改成下面这样:

    java.io.Reader reader = null;
    try {
        reader = new InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream("definitions/level_definitions.txt"););
    } finally {
    } 
    is = new BufferedReader(reader);
    
    
    
    // blocks txt
    reader = new InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream(stringPath);); 
    is = new BufferedReader(reader);
    
    // images
    File pathToFile = new File("resources/" + stringPath);
    try {
       backgroundImage = ImageIO.read(ClassLoader.getSystemClassLoader().getResourceAsStream(stringPath););
    } catch (IOException e) {
       e.printStackTrace();
    }

但是当然,它不起作用,程序根本就没有运行。

问题是,有人明白我的教授们的确切意思吗?关于如何相应地更改我的代码以使其“相对于类路径”的任何建议??

谢谢!

一个资源不是文件系统File。资源与 .class 文件一起驻留在 .jar 中。

Path 是文件、资源等的概括。)

资源路径 "definitions/level_definitions.txt" 似乎是正确的。如果您构建一个 .jar,您可以使用 WinZip、7zip 或其他工具检查它,因为它只是一个 zip 文件。 应该有一个顶级目录 definitions.

(可能 resources 是非 java 资源的根目录。)

路径必须 区分大小写,分隔符必须是正斜杠 (/)。

Windows 上一个非常常见的错误是未显示扩展名,并且创建了类似 level_definitions.txt.txt.

的文件

InputStreamReader 包装二进制数据 InputStream 并作为 Reader returns 文本。 为此,可以添加二进制数据的编码 (Charset)。你使用默认的没有编码,所以它使用平台编码。

如果您的程序在另一个平台上运行,它仍然使用相同的资源文本文件,但可能应用了错误的编码。所以最好使用资源文本的编码:对于意大利“Windows-2152”或全球“UTF-8”。好好检查一下cafè.