0

I am trying to load some data saved in my property file in an Android Application.

I have put my property file under the src folder. Every time I try to load data from my file it keeps telling me FileNotFoundException open failed ENOENT (No such file or directory).

My Code is as follows:

This code is to save the file (new create)

      File file = new File("src/com/example/testphonegap/SilverAngel.properties");
      FileOutputStream fileOut = new FileOutputStream(file);
      properties.store(fileOut, "Properties");
      fileOut.close();

This code is to load data

    properties = new Properties();
    InputStream is = null;

     // First try loading from the current directory
    try {
        File f = new File("src/com/example/testphonegap/SilverAngel.properties");
        is = new FileInputStream(f);

        // Try loading properties from the file (if found)
        properties.load(is);

        GetPersonaliseSettings();
        GetUserSettings();
        GetFavSettings();
    }
    catch ( Exception e ) { 
        is = null; 
    }

Can you tell me what I am doing wrong please? Is it where the file is saved or am I missing something in my code?

4
  • have you tried changing the directory to a location outside like in a newfolder in the external memory Commented Aug 26, 2014 at 11:22
  • No.. How can I do that please Commented Aug 26, 2014 at 11:24
  • File f = new File(getApplicationContext().getFilesDir()+"/YourFolderName"); Commented Aug 26, 2014 at 11:26
  • Put that file in the projectDirectory/res/raw, instead of src. Commented Aug 26, 2014 at 11:31

3 Answers 3

2

It's an Android Application, so the file is stored on the Android device and not on your computer. If you want to save the file on the SD card you can write the following:

String root = Environment.getExternalStorageDirectory().toString();
File dir = new File(root + "/subfolderForYourApp");
dir.mkdirs();

This will create a subfolder for your application on the SD card. To create a new file in this directory:

File file = new File(dir, "SilverAngel.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Properties");
fileOut.close();

Don't forget to add the permission to the manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. Appreciated
0

try this

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("com/example/testphonegap/SilverAngel.properties");
properties.load(inputStream);

make sure you don't add "src" before property file name.

Comments

0

Put it in the assets folder and load it from there.

AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("SilverAngel.properties");
properties.load(inputStream);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.