簡體   English   中英

Blackberry讀取項目中的本地屬性文件

[英]Blackberry read local properties file in project

我在黑莓項目的根目錄下有一個config.properties文件(與Blackberry_App_Descriptor.xml文件位於同一位置),我嘗試訪問該文件以對其進行讀寫。 見下面我的課:

public class Configuration {
private String file;
private String fileName;

public Configuration(String pathToFile) {
    this.fileName = pathToFile;

    try {
        // Try to load the file and read it
        System.out.println("---------- Start to read the file");
        file = readFile(fileName);
        System.out.println("---------- Property file:");
        System.out.println(file);
    } catch (Exception e) {
        System.out.println("---------- Error reading file");
        System.out.println(e.getMessage());
    }
}

/**
 * Read a file and return it in a String
 * @param fName
 * @return
 */
private String readFile(String fName) {
    String properties = null;

    try {
        System.out.println("---------- Opening the file");
        //to actually retrieve the resource prefix the name of the file with a "/"
        InputStream is = this.getClass().getResourceAsStream(fName);

        //we now have an input stream. Create a reader and read out
        //each character in the stream.
        System.out.println("---------- Input stream");
        InputStreamReader isr = new InputStreamReader(is);

        char c;

        System.out.println("---------- Append string now");
        while ((c = (char)isr.read()) != -1) {
            properties += c;
        }
    } catch (Exception e) {

    }

    return properties;
}

}

我這樣稱呼我的類構造函數:

Configuration config = new Configuration("/config.properties");

因此,在我的課程中,“文件”應具有config.properties文件的所有內容,而文件名應具有此值“ /config.properties”。

但是“名稱”為空,因為找不到文件...我知道這是文件的路徑,應該不同,但是我不知道可以更改什么...該類在com包中.mycompany.blackberry.utils

謝謝!

我認為您在構建項目時需要將config.properties文件放到源文件夾中,您可以將“ resources”文件夾創建為src文件夾並將配置文件放入其中,而不是在應用程序中獲取文件。

嘗試將文件與類放在同一包中?

Class clazz = Class.forName("Configuration");
InputStream is = addFile.getResourceAsStream(fName);

暫無
暫無

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

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