Suppose we want to manage some different parameters to configure a given application, and make easy to modify these ones without the need to recompile the entire project.
Just think about the configuration parameters for the connection to a web page or to a given server: if the ip address changes and it is hard-coded within the application, we need to modify it in the code and recompile befor running again.
In this cases it can be useful to create a properties file to trace the most important parameters related to the connection.
We have to work with couples of strings of the form <key,value>, where the key is used to retrieve the correspondent value.
Suppose that our file configuration.properties is the following:
# Configuration File
Suppose that our file configuration.properties is the following:
# Configuration File
ip=95.678.191.255
port=8799
host=pillsfromtheweb.blogspot.it
The class we have to realize to open and read the file is the following class PropertiesReader.java (we assume that the file has been inserted in the assets folder):
public class PropertiesReader {
private Context context;
private Properties properties;
public PropertiesReader(Context context) {
this.context = context;
//creates a new object ‘Properties’
properties = new Properties();
public Properties getProperties(String FileName) {
try {
//access to the folder ‘assets’
AssetManager am = context.getAssets();
//opening the file
InputStream inputStream = am.open(FileName);
//loading of the properties
properties.load(inputStream);
}
catch (IOException e) {
Log.e("PropertiesReader",e.toString());
try {
//access to the folder ‘assets’
AssetManager am = context.getAssets();
//opening the file
InputStream inputStream = am.open(FileName);
//loading of the properties
properties.load(inputStream);
}
catch (IOException e) {
Log.e("PropertiesReader",e.toString());
}
}
return properties;
return properties;
}
}
}
After reading the file, we have to retrieve the values of the connection parameters within through the method readPropertiesFromFile():
private void readPropertiesFromFile(Context context) {
//reads the configuration file
//reads the configuration file
propertiesReader = new PropertiesReader(context);
p=propertiesReader.getProperties("configuration.properties");
//recovery of the parameters
ip_address = p.getProperty("ip");
hostname = p.getProperty("host");
port = p.getProperty("port");
p=propertiesReader.getProperties("configuration.properties");
//recovery of the parameters
ip_address = p.getProperty("ip");
hostname = p.getProperty("host");
port = p.getProperty("port");
}
Hi I thank you the content, but your code is unnorganized I share here an update of your code with a singleton design pattern
ReplyDeleteReader
public class PropertiesReader {
private Context context;
private Properties properties;
private static PropertiesReader pr;
private PropertiesReader(Context context) {
this.context = context;
properties = new Properties();
}
public static PropertiesReader getInstance(Context context){
if(pr == null) return new PropertiesReader(context);
else return pr;
}
public Properties getProperties(String fileName){
try {
//access to the folder ‘assets’
AssetManager am = context.getAssets();
//opening the file
InputStream inputStream = am.open(fileName);
//loading of the properties
properties.load(inputStream);
} catch (IOException e) {
Log.e("PropertiesReader", e.toString());
}
return properties;
}
}
Properties propFile = PropertiesReader.getInstance(getApplicationContext()).getProperties("fileName.properties");