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.
Author by
user2154477
Updated on June 04, 2022Comments
-
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 almost 11 yearsTRAP: this will read the file as ISO-8859-1! If you want to read it as UTF-8, wrap the
InputStream
into anInputStreamReader
with the correct character set (Properties
has a.load()
method accepting to read from a reader) -
user2154477 almost 11 yearsWhats the difference if I just want to just get a number from the file and assign it to the variable?
-
PandaBearSoup almost 11 yearsIf 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 almost 11 yearsI 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 almost 11 yearsIf you are only using alphanumeric characters then you should have no problems with my example code.
-
user2154477 almost 11 yearsWhere 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 almost 10 yearswell this answer is useless if you do not know where to put your prop file.
-
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 over 9 yearsHere there is explained clearly: pillsfromtheweb.blogspot.com/2014/09/…