android下properties文件的读写操作

   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.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import android.app.Activity;

/**
 * 读取本地保存的proper文件,保存的行为记录
 * 
 * @author qiulinhe
 * @createTime 2016年6月7日 下午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 = null;
		try {
			in = new FileInputStream(android.os.Environment.getExternalStorageDirectory() + File.separator
					+ "operation.properties");
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		try {
			props.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return props;
	}

	/**
	 * 给属性文件添加属性
	 * 
	 * @param param
	 * @param value
	 * @author qiulinhe
	 * @createTime 2016年6月7日 下午1:46:53
	 */
	public static void put(Activity activity, String key, String value) {
		String PROP_PATH = activity.getApplicationContext().getFilesDir().getAbsolutePath();
		Properties p = new Properties();
		try {
			InputStream in = new FileInputStream(android.os.Environment.getExternalStorageDirectory() + File.separator
					+ "operation.properties");
			p.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		p.setProperty(key, value);
		OutputStream fos;
		try {
			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卡上

发布了307 篇原创文章 · 获赞 214 · 访问量 173万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览