Reading properties file

0

How do I read data from a properties file on Android? Here's my code for onCreate :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File file = new File(getPackageName()+ "/dados.properties");
    Properties pp = new Properties();
    FileInputStream fis = null;
    try {       
        fis = new FileInputStream(file);
        pp.load(fis);
        fis.close();
    } catch (Exception e) { }
}

It always gives error in fis = new FileInputStream(file); .

    
asked by anonymous 13.06.2014 / 22:46

2 answers

1

I recommend putting the dados.properties file in the res/raw folder. By placing this folder you can get a InputStream for any file in this folder with this code:

public InputStream readRaw(Context context, int rawResId) {
    return context.getResources().openRawResource(rawResId);
}

If you'd like more details at a glance at the documentation for Resources.openRawResource(int id)

Embedding in your code would look like:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Properties pp = new Properties();

    try {
        pp.load(readRaw(this, R.raw.dados));
    } catch (Exception e) { }
}
    
14.06.2014 / 02:15
0

The path you are looking for is not the one you are looking for. Take a look at the getFilesDir function, it will return the path of the files of your application (/ date / data / seupacote).

Try something like this:

File file = new File(getFilesDir().getAbsolutePath() + "/dados.properties");
    
16.06.2014 / 11:08