How to load .properties file data into an Android studio project

In my current application, I need to support one config.properties , and from this properties file I need to get the data in my java file. I put the properties file and ConfigUtil.java , which accesses these properties files, is in one place. But when I launch the application, it gives a FileNotFoundException .

I cannot understand why this does not load the properties file if both are inside the same folder.

My ConfigUtils.java code:

 public class ConfigUtil { private Properties properties; InputStream inputStream = null; public Properties getUrl(){ properties = new Properties(); try { inputStream = new FileInputStream("config.properties"); properties.load(inputStream); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(inputStream != null){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return properties; } } 

and the config.properties file is also located in the same folder as config.properties :

/app/src/main/java/config.properties

ConfigUtil.java location:

/app/src/main/java.configutils.java

+5
source share
2 answers

Step 1

Create a .properties file in the resource folder, if you do not have a resource folder, create it under the main

enter image description here

enter image description here

config.properties

 name=User Name age=User Age ok=Click 

Step 2

Create a Util.java file to read the properties file.

Util.java

 package javaant.com.propertiesfile; import android.content.Context; import android.content.res.AssetManager; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * Created by Nirmal Dhara on 12-07-2015. */ public class Util { public static String getProperty(String key,Context context) throws IOException { Properties properties = new Properties();; AssetManager assetManager = context.getAssets(); InputStream inputStream = assetManager.open("config.properties"); properties.load(inputStream); return properties.getProperty(key); } } 

Step 3

Use the variables in the properties file by calling Util.getProperty (, getApplicationContext ()).

MainActivity.java

 package javaant.com.propertiesfile; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageButton; import android.widget.TextView; import java.io.IOException; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // read value from properties file TextView txtname= (TextView) findViewById(R.id.txtname); TextView txtage= (TextView) findViewById(R.id.txtage); Button btnok= (Button) findViewById(R.id.btnok); try { txtname.setText(Util.getProperty("name",getApplicationContext())); txtage.setText(Util.getProperty("age",getApplicationContext())); btnok.setText(Util.getProperty("ok",getApplicationContext())); } catch (IOException e) { e.printStackTrace(); } } } 

Download the full code here http://javaant.com/how-to-use-properties-file-in-android/#.VwzippN96Hs

+27
source

You must either specify the full path to the file (since the root working directory is not the directory where ConfigUtil is located):

 inputStream = new FileInputStream("src/main/java/config.properties"); 

or use the following (which uses the ConfigUtil directory):

 inputStream = getClass().getResourceAsStream("config.properties"); 
0
source

Source: https://habr.com/ru/post/1201065/


All Articles