在我当前的应用程序中,我需要维护一个config.properties
文件,并且从这个属性文件中我需要获取我的java文件中的数据.我已经放置了属性文件,ConfigUtil.java
并且正在访问这些属性文件的值位于同一位置.但是,当我运行应用程序时,它正在给予FileNotFoundException
.
当两者都在同一个文件夹中时,我无法理解为什么没有加载属性文件.
我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
和config.properties文件也在同一文件夹位置config.properties
:
/app/src/main/java/config.properties
位置ConfigUtil.java
是:
/app/src/main/java.configutils.java
Nir*_*ara 35
步骤1
在assets文件夹中创建一个.properties文件,如果你没有assets文件夹,请在main下创建一个.properties文件
config.properties
name=User Name
age=User Age
ok=Click
Run Code Online (Sandbox Code Playgroud)
第2步
创建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);
}
}
Run Code Online (Sandbox Code Playgroud)
第3步
通过调用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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
请从这里下载完整的代码http://javaant.com/how-to-use-properties-file-in-android/#.VwzippN96Hs
您应该指定文件的完整路径(因为根工作目录不是ConfigUtil
所在目录):
inputStream = new FileInputStream("src/main/java/config.properties");
Run Code Online (Sandbox Code Playgroud)
或使用以下命令(使用ConfigUtil
从中加载的目录):
inputStream = getClass().getResourceAsStream("config.properties");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21675 次 |
最近记录: |