簡體   English   中英

相對路徑 Java

[英]Relative Path Java

我在 Java 中的分配遇到了一些麻煩。 這是一個從 txt 文件中讀取級別定義的程序。 它還讀取圖像。 它的工作原理是讓我有一個“資源”文件夾(在程序文件夾內),里面有我需要的所有文件夾/文件。 以下是資源文件夾的示例:

資源

背景圖像

雲.png

叢林.jpg

夜晚.jpg

塊圖像

豹.jp

斑馬.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 文件中,可能位於 a.jar 中。

Path是文件、資源甚至更多的概括。)

資源路徑"definitions/level_definitions.txt"似乎是正確的。 如果您構建一個.jar,您可以使用 WinZip、7zip 或其他工具對其進行檢查,因為它只是一個 zip 文件。 應該有一個頂級目錄definitions

(可能resources是非 java 資源的根目錄。)

路徑必須區分大小寫,分隔符必須是正斜杠 ( / )。

Windows 上的一個非常常見的錯誤是未顯示擴展名,並且創建了像level_definitions.txt.txt這樣的文件。

InputStreamReader 包裝二進制數據 InputStream 並作為 Reader 返回文本。 為此,可以添加二進制數據的編碼( Charset )。 您使用不帶編碼的默認值,因此它使用平台編碼。

如果您的程序在另一個平台上運行,它仍然使用相同的資源文本文件,但可能會應用錯誤的編碼。 所以最好使用資源文本的編碼:對於意大利“Windows-2152”或全局“UTF-8”。 檢查它與一個好的cafè

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM