Blackberry reads local properties file in project

I have a config.properties file in the root of my blackberry project (same location as Blackberry_App_Descriptor.xml file) and I am trying to access the file to read and write to it. See below my 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;
}

      

}

I am calling my class constructor like this:

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

      

So in my class "file" should have all the contents of the config.properties file and filename should have this value "/config.properties".

But "name" is null because the file cannot be found ... I know this is the path to the file, which should be different, but I don't know what I can change ... The class is in the com. mycompany.blackberry.utils

Thanks!

+2


a source to share


3 answers


I think you need to put the config.properties file in the source folder when you create the project, you can create the "resources" folder as a src folder and put the config file in it than you can get the file in the application



+1


a source


Try putting the file in the same package as the class?



0


a source


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

      

0


a source







All Articles