views:

203

answers:

2

Hi, I have a config.properties file at the root of my blackberry project (same place as Blackberry_App_Descriptor.xml file), and I try to access the file to read and write into 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 call my class constructor like this:

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

So in my class, "file" should have all the content of the config.properties file, and the fileName should have this value "/config.properties".

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

Thank you!

A: 

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

bryanallott
I don't want to put my configuration file in package somewhere, it needs to be easily accessible in the project for the future.
Dachmt
the reason being the path "/config.properties"- if you tried putting it there does it find the file? and if not, then there's something wrong with the packaging/project setup? the code looks fine. i use something very similar for picking up EULA texts and it works for me
bryanallott
A: 

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

Sherlock