如何在Android开发中对java properties读取文件进行读取

1936人阅读
Android(239)
& &android下properties文件的读写操作,网上大多数是读是成功的,如果要是写数据到文件里面,都会报这样的错误:Caused by: java.io.FileNotFoundException: /operation.properties: open failed: EROFS (Read-only file system),提示是没有权限进行写,如下是国外的网站的解释:
... saves to an external directory
This is not quite right. You are trying to save it in the root system partition which is always read-only.
From javadoc of Environment.getRootDirectory():
Return root of the &system& partition holding the core Android OS. Always present and mounted read-only.
Solution : just save your file somewhere else :
StreamResult result = new StreamResult(new File(android.os.Environment.getExternalStorageDirectory(), &upload_data.xml&));
Note that :
you must ask the permission to write to the external storage :
&uses-permission android:name=&android.permission.WRITE_EXTERNAL_STORAGE& /&
External storage is not always available. The partition may not be mounted -yet- when you try to write on it. (just be careful)
android.os.Environment.getExternalStorageDirectory()& & &上述的解释大概是:root system文件系统是只读的,需要使用android.os.Environment.getExternalStorageDirectory()这样的形式,获取扩展存储设备的文件目录
  * android.os.Environment.getExternalStorageDirectory();可以进行读写,所以我将网上的代码的路径都改为这个路径,不将properties的文件放到src目录下,如下代码:
package allone.verbank.apad.client.
import java.io.F
import java.io.FileInputS
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
import java.io.InputS
import java.io.OutputS
import java.util.P
import android.app.A
* 读取本地保存的proper文件,保存的行为记录
* @author qiulinhe
* @createTime 日 下午1:28:16
public class ProperUtil {
* 得到netconfig.properties配置文件中的所有配置属性
* @return Properties对象
public static Properties getNetConfigProperties() {
Properties props = new Properties();
InputStream in = ProperUtil.class.getResourceAsStream(&/operation.properties&);
InputStream in =
in = new FileInputStream(android.os.Environment.getExternalStorageDirectory() + File.separator
+ &operation.properties&);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
props.load(in);
} catch (IOException e) {
e.printStackTrace();
* 给属性文件添加属性
* @param param
* @param value
* @author qiulinhe
* @createTime 日 下午1:46:53
public static void put(Activity activity, String key, String value) {
String PROP_PATH = activity.getApplicationContext().getFilesDir().getAbsolutePath();
Properties p = new Properties();
InputStream in = new FileInputStream(android.os.Environment.getExternalStorageDirectory() + File.separator
+ &operation.properties&);
p.load(in);
} catch (IOException e) {
e.printStackTrace();
p.setProperty(key, value);
fos = new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + File.separator
+ &operation.properties&);
p.store(fos, null);
} catch (Exception e) {
throw new RuntimeException(e);
如下路径的解释:
其实Android&Environment.getExternalStorageDirectory()&获取的&SD卡和手机本身带的存储&;这两个存储都是外部储存,真正的内部储存位置是data/data/包名,所以用了这个会出现还是存到了手机自带存储上,而不是SD卡上
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:534149次
积分:8126
积分:8126
排名:第2549名
原创:235篇
转载:107篇
评论:190条
(1)(2)(8)(5)(10)(3)(14)(4)(12)(20)(17)(18)(10)(6)(14)(14)(11)(7)(15)(8)(15)(9)(16)(10)(15)(17)(24)(1)(3)(41)Android local.properties 文件读取实例详解
作者:赵彦军
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Android local.properties 文件读取实例详解的相关资料,需要的朋友可以参考下
Android local.properties 文件读取实例详解
在Android Studio项目里面有个local.properties文件,这个文件可以放一些系统配置。比如:sdk路径、ndk路径。
ndk.dir=D\:\\soft\\android-ndk-r10e
sdk.dir=D\:\\soft\\SDKandroidStudio
当然我们也可以在local.properties放一些自定义的配置,比如签名文件:
key.file=C\:\\work\\Key.jks
keyAlias=key
keyPassword=key7766
storePassword=key6677
build.gradle 如何读取local.properties字段信息
signingConfigs {
//加载资源
Properties properties = new Properties()
InputStream inputStream = project.rootProject.file('local.properties').newDataInputStream() ;
properties.load( inputStream )
//读取文件
def sdkDir = properties.getProperty('key.file')
storeFile file( sdkDir )
//读取字段
def key_keyAlias = properties.getProperty( 'keyAlias' )
def key_keyPassword = properties.getProperty( 'keyPassword' ) ;
def key_storePassword = properties.getProperty( 'storePassword' ) ;
storePassword key_storePassword
keyAlias key_keyAlias
keyPassword key_keyPassword
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具Android中读取properties文件
通过流文件来进行properties文件读取的,要将文件放入到assets文件夹或者raw文件夹中.例如,我们这里有一个文件test.properties,如果放入了assets文件夹中,可以如下打开:
Properties pro = new Properties();&
InputStream is = context.getAssets().open(&test.properties&);&&
pro.load(is);&
&&& 如果放入到raw文件夹中,可以通过如下方式打开:
InputStream is = context.getResources().openRawResource(R.raw.test);&
Properties pro = new Properties();&
pro.load(FileLoad.class.getResourceAsStream(&/assets/test.properties&));&
读写函数分别如下:
import java.io.FileInputS&
import java.io.FileOutputS&
import java.util.P&
public Properties loadConfig(Context context, String file) {&
Properties properties = new Properties();&
FileInputStream s = new FileInputStream(file);&
properties.load(s);&
} catch (Exception e) {&
e.printStackTrace();&
public void saveConfig(Context context, String file, Properties properties) {&
FileOutputStream s = new FileOutputStream(file, false);&
properties.store(s, &&);&
} catch (Exception e){&
e.printStackTrace();&
orz,是不是发现什么了?对了,这两个函数与一点关系都没有嘛。。
所以它们一样可以在其他标准的java程序中被使用
在Android中,比起用纯字符串读写并自行解析,或是用xml来保存配置,
Properties显得更简单和直观,因为自行解析需要大量代码,而xml的操作又远不及Properties方便
使用方法如下:
写入配置:
Properties prop = new Properties();
prop.put(&prop1&, &abc&);
prop.put(&prop2&, 1);
prop.put(&prop3&, 3.14);
saveConfig(this, &/sdcard/config.dat&, prop);
读取配置:
Properties prop = loadConfig(this, &/sdcard/config.dat&);
String prop1 = prop.get(&prop1&);
注:也可以用Context的openFileInput和openFileOutput方法来读写文件
此时文件将被保存在 /data/data/package_name/files下,并交由统一管理
用此方法读写文件时,不能为文件指定具体路径。
&&& 在android中,当我们打包生成apk后,将apk放入到真正的手机上时,你会找不到test.properties文件,不要惊讶,android中的资源文件是只能存放在assets或者res的子目录里面的,程序包中的资源文件编译后,是会丢失的!那么是不是我们的第二种方法就没法使用了?当然不是,经过实验发现,将文件放入到assets文件夹里,而在传入路径里面填入文件绝对路径,还是可以引用该文件的.Android 的Gradle项目中最近常见local.properties,这个玩意儿是怎么来的? - 知乎24被浏览8287分享邀请回答1添加评论分享收藏感谢收起3添加评论分享收藏感谢收起查看更多回答2010年12月 移动平台大版内专家分月排行榜第二2010年11月 移动平台大版内专家分月排行榜第二
2011年5月 移动平台大版内专家分月排行榜第三2011年4月 移动平台大版内专家分月排行榜第三2011年3月 移动平台大版内专家分月排行榜第三
2011年2月 移动平台大版内专家分月排行榜第三2011年1月 移动平台大版内专家分月排行榜第三2010年10月 移动平台大版内专家分月排行榜第三
2011年2月 移动平台大版内专家分月排行榜第三2011年1月 移动平台大版内专家分月排行榜第三2010年10月 移动平台大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 properties文件的读取 的文章

 

随机推荐