简体   繁体   English

相对路径 Java

[英]Relative Path Java

I've been having some trouble with an assigment in Java.我在 Java 中的分配遇到了一些麻烦。 It's a program that reads level-definitions from txt files.这是一个从 txt 文件中读取级别定义的程序。 It also reads images.它还读取图像。 It works so that I have a "resources" folder (inside the program folder) and inside it all the folders/files that I need.它的工作原理是让我有一个“资源”文件夹(在程序文件夹内),里面有我需要的所有文件夹/文件。 Here is an example of a resource folder:以下是资源文件夹的示例:

resources资源

background_images背景图像

clouds.png云.png

jungle.jpg丛林.jpg

night.jpg夜晚.jpg

block_images块图像

leopard.jp豹.jp

zebra.jpg斑马.jpg

definitions定义

level_definitions.txt level_definitions.txt

Inside the level_definitions.txt , there are all kinds of names of other files to which you should redirect, such aslevel_definitions.txt中,您应该重定向到的其他文件的各种名称,例如

background_images/night.jpg  or  definitions/moon_block_definitions.txt

This is the program pretty much, and I was able to code it, it works just fine.这几乎是程序,我能够编写代码,它工作得很好。

At the end of the assignment though, they added this note:不过,在作业结束时,他们添加了以下注释:

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.

I really have no idea what they meant by this, and how should I change my code.我真的不知道他们的意思是什么,以及我应该如何更改我的代码。 (my professors are not answering and the assignment is due in just a few days). (我的教授没有回答,几天后作业就到期了)。

Here is how my code reads the files right now:这是我的代码现在读取文件的方式:

This is how I read the main file:这就是我阅读主文件的方式:

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);

This is how I read the other files:这就是我阅读其他文件的方式:

// 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();
}

Now, I have tried to change into the following:现在,我尝试更改为以下内容:

    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();
    }

But of course, it's not working, the program simply doesn't run.但是,当然,它不起作用,程序根本无法运行。

The question is, anyone understands what my professors exactly meant?问题是,有人明白我的教授的确切意思吗? Any advice on how to change my code accordingly so that it is "relative to classpath"??关于如何相应地更改我的代码以使其“相对于类路径”的任何建议?

Thank you!谢谢!

A resource is not file system File .资源不是文件系统File Resources reside with the.class files possibly inside a.jar.资源位于 .class 文件中,可能位于 a.jar 中。

( Path is a generalisation of both File, resources and even more.) Path是文件、资源甚至更多的概括。)

The resource path "definitions/level_definitions.txt" seems correct.资源路径"definitions/level_definitions.txt"似乎是正确的。 If you build a.jar, you might inspect it with WinZip, 7zip or other tools, as it is just a zip file.如果您构建一个.jar,您可以使用 WinZip、7zip 或其他工具对其进行检查,因为它只是一个 zip 文件。 There should be a top level directory definitions .应该有一个顶级目录definitions

(Probably resources is a root directory for non-java resources.) (可能resources是非 java 资源的根目录。)

The path must be case sensitive and the separator the forward slash ( / ).路径必须区分大小写,分隔符必须是正斜杠 ( / )。

One pretty frequent error on Windows is that the extension is not shown and one gets created files like level_definitions.txt.txt . Windows 上的一个非常常见的错误是未显示扩展名,并且创建了像level_definitions.txt.txt这样的文件。

The InputStreamReader wraps a binary data InputStream and as Reader returns text. InputStreamReader 包装二进制数据 InputStream 并作为 Reader 返回文本。 For that one can add the encoding ( Charset ) of the binary data.为此,可以添加二进制数据的编码( Charset )。 You use the default without encoding, so it uses the platform encoding.您使用不带编码的默认值,因此它使用平台编码。

If your program runs on another platform it still uses the same resource text file, but might apply a wrong encoding.如果您的程序在另一个平台上运行,它仍然使用相同的资源文本文件,但可能会应用错误的编码。 So better use the encoding of the resource text: for Italy "Windows-2152" or the global "UTF-8".所以最好使用资源文本的编码:对于意大利“Windows-2152”或全局“UTF-8”。 Check it with a good cafè .检查它与一个好的cafè

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

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