Java 如何在android studio项目中加载.properties文件数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25483735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load .properties file data in android studio project
提问by anand
In my current application i need to maintain one config.properties
file and from this properties file i need to get the data in my java file. I have placed the properties file and ConfigUtil.java
which is accessing those properties files values are in the same location. But when i am running the application it's giving FileNotFoundException
.
在我当前的应用程序中,我需要维护一个config.properties
文件,我需要从这个属性文件中获取我的 java 文件中的数据。我已经放置了属性文件,ConfigUtil.java
并且正在访问这些属性文件的值位于同一位置。但是当我运行应用程序时,它给出了FileNotFoundException
.
I am not able to get why this is not loading the properties file when both are inside the same folder.
当两者都在同一个文件夹中时,我无法理解为什么这不加载属性文件。
My ConfigUtils.java code is :
我的 ConfigUtils.java 代码是:
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 config.properties file is also in same folder
location of config.properties
is :
和 config.properties 文件也位于 is 的同一文件夹位置config.properties
:
/app/src/main/java/config.properties
/app/src/main/java/config.properties
location of ConfigUtil.java
is :
位置ConfigUtil.java
是:
/app/src/main/java.configutils.java
/app/src/main/java.configutils.java
回答by nikis
You should either specify the full path to the file (because the root working directory is not the directory where ConfigUtil
is located) :
您应该指定文件的完整路径(因为根工作目录不是所在的目录ConfigUtil
):
inputStream = new FileInputStream("src/main/java/config.properties");
or use the following (which is using the directory ConfigUtil
was loaded from):
或使用以下内容(使用ConfigUtil
从中加载的目录):
inputStream = getClass().getResourceAsStream("config.properties");
回答by Nirmal Dhara
Step 1
第1步
Create a .properties file in assets folder, if you don't have assets folder please create one under main
在assets文件夹中创建一个.properties文件,如果没有assets文件夹请在main下创建一个
config.properties
config.properties
name=User Name
age=User Age
ok=Click
Step 2
第2步
Create a Util.java file to read the properties file.
创建 Util.java 文件以读取属性文件。
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
第 3 步
Use variables in the properties file by calling Util.getProperty(,getApplicationContext()).
通过调用 Util.getProperty(,getApplicationContext()) 使用属性文件中的变量。
MainActivity.java
主活动.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();
}
}
}
Please download the complete code from here http://javaant.com/how-to-use-properties-file-in-android/#.VwzippN96Hs
请从这里下载完整的代码http://javaant.com/how-to-use-properties-file-in-android/#.VwzippN96Hs