Loading Properties File in Android

16,596

Here is some example code...

Properties props=new Properties();
InputStream inputStream = 
this.getClass().getClassLoader().getResourceAsStream("example.properties");
props.load(inputStream);
exampleUrl=props.getProperty("baseUrl");
exampleUsername=props.getProperty("username");      
examplepassword=props.getProperty("password");

can not help much more without some of your own code. Hope this helps.

Share:
16,596
user2154477
Author by

user2154477

Updated on June 04, 2022

Comments

  • user2154477
    user2154477 almost 2 years

    I have a properties file in my android project but I don't know how to read properties from the file. I have seen several tutorials but those did not help me, in addition I am getting some errors. Would you please post some simple example code to understand how this works?

  • fge
    fge almost 11 years
    TRAP: this will read the file as ISO-8859-1! If you want to read it as UTF-8, wrap the InputStream into an InputStreamReader with the correct character set (Properties has a .load() method accepting to read from a reader)
  • user2154477
    user2154477 almost 11 years
    Whats the difference if I just want to just get a number from the file and assign it to the variable?
  • PandaBearSoup
    PandaBearSoup almost 11 years
    If you are just getting numbers you should be fine. ISO-8859-1 can represent only the first 256 Unicode characters, the more popularly used ones. UTF-8 can represent all Unicode characters.
  • user2154477
    user2154477 almost 11 years
    I have a file that has several of these on different lines with different words and numbers "counter=0". I really just want a way to write to this file and read from this file so that it can store numbers needed even after the application is closed.
  • PandaBearSoup
    PandaBearSoup almost 11 years
    If you are only using alphanumeric characters then you should have no problems with my example code.
  • user2154477
    user2154477 almost 11 years
    Where should I place my properties file inside my project? I just got a chance to add in the code and I get a null pointer exception for the InputStream when i use the properties.load() method.
  • PKlumpp
    PKlumpp almost 10 years
    well this answer is useless if you do not know where to put your prop file.
  • PandaBearSoup
    PandaBearSoup almost 10 years
    @ZerO The op had a properties file. I answered his request for example code to better understand reading properties. Why are you responding to this a year later? If you don't know where to put your properties folder than ask that question. The method I wrote assumes the properties file is in the same folder as the Class you are calling it from.
  • SegFault
    SegFault over 9 years
    Here there is explained clearly: pillsfromtheweb.blogspot.com/2014/09/…