1

I have a file defined next to the default.properties, AndroidManifest.xml, called my_config.properties. my question is. how do open this file in my class?

if i move it to the class package i can read it using the folowing code:

 Properties configFile = new Properties();
    try {
        configFile.load(MyConstantsClass.class.getResourceAsStream("my_config.properties"));
    } catch (IOException e) {
        e.printStackTrace();
    }

but in this case the file needs to be inside the same package as the class i use this snippet in. how do i read from it when its defined like in the beggining of my question? Thank you.

DArkO
  • 15,880
  • 12
  • 60
  • 88

2 Answers2

2

You can only open files from Android that are included in your APK, the current location of the my_config.properties will not be included in there. I would suggest that the right place for this kind of file would be your "assets" directory and you have to use the correct class to access it.

Robert Massaioli
  • 13,379
  • 7
  • 57
  • 73
  • thanks. but is there any chance that i can define something like this in build time. i don't need those properties in the apk. i just need to compile the app according to a prod/test profile. for example i will have one constant defined true as test and false as prod. i need this because i use a different base url for the production and test server. is there a way this sort of thing can be handled better than using if clauses, manually going over the code before release to make all the changes for production or in a .java class? – DArkO May 17 '11 at 11:33
  • 1
    In that case if you want to know if you are on a production or development environment then you would be better off looking at this question: http://stackoverflow.com/questions/1743683/distinguishing-development-mode-and-release-mode-environment-settings-on-android Otherwise I do not know how you would do what you would want to do on Android. – Robert Massaioli May 17 '11 at 12:32
0

you able get the value from .properties file the example's below:

    Properties prop = new Properties();
    String propertiesPath = this.getFilesDir().getPath().toString() + "/app.properties";

    try {

        File existFile = new File(propertiesPath);

        if(existFile.isFile())
        {
            FileInputStream inputStream = new FileInputStream(propertiesPath);
            prop.load(inputStream);
            inputStream.close();    
        }   

    } catch (IOException e) {
        System.err.println("Failed to open app.properties file");
        e.printStackTrace();
    }

Aey.Sakon

user3113670
  • 1,909
  • 1
  • 12
  • 8