gpt4 book ai didi

java - 黑莓读取项目中的本地属性文件

转载 作者:行者123 更新时间:2023-11-30 11:59:00 26 4
gpt4 key购买 nike

我的 blackberry 项目的根目录下有一个 config.properties 文件(与 Blackberry_App_Descriptor.xml 文件相同的位置),我尝试访问该文件以读取和写入该文件。请参阅下面我的类(class):

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

所以在我的类中,“file”应该有config.properties文件的所有内容,fileName应该有这个值“/config.properties”。

但是“名称”为空,因为找不到该文件...我知道这是应该不同的文件路径,但我不知道我可以更改什么...该类在包 com.mycompany.blackberry.utils 中

谢谢!

最佳答案

我认为你需要在构建项目时将config.properties文件放入源文件夹中,你可以创建一个“resources”文件夹作为src文件夹并将配置文件放入其中,然后你就可以获得文件在应用程序中

关于java - 黑莓读取项目中的本地属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2791369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com