写这篇文章之前可以成功运行,文章后就各种找不到文件.所以并没有采用此种方式,后期完善.详见下篇解决方案.

配置文件读取很容易,修改需要注意权限,比如assets目录下就不允许修改.

配置文件的创建:

New --- File

命名后选择properties方式打开

配置文件设置

contrastIP = 192.166.1.65:8011

assets目录创建

在main目录下,与java res 目录同级创建.

New --- Folder --- Assets Folder

assets目录详解: http://blog.csdn.net/chuntiandejiaobu10/article/details/52352128

权限配置

在 AndroidManifest.xml 中添加:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

其实我去掉后测试也可以成功运行.还是加上.预防万一.

先读配置:

方法一 读取assets目录下的配置

Properties props = new Properties();
props.load(context.getAssets().open(configName));

将configName文件从assets目录下放出来,放在main目录下:

方法二  会出现错误 open failed: ENOENT (No such file or directory) 然而并不知道目录路径该如何填

Properties props = new Properties();
props.load(new FileInputStream(configName));

方法三  这样就能成功运行

Properties props = new Properties();
props.load(context.openFileInput(configName));

修改配置:

Properties props = new Properties();
props.load(context.openFileInput(configPath));
props.setProperty(keyName, keyValue); // 读取assets目录下的,但是根本无法修改
// FileOutputStream out = context.getAssets().openFd(configPath).createOutputStream();
// 提示 open failed: EROFS (Read-only file system) 
// FileOutputStream out = new FileOutputStream(configPath); // 这样就可以了
FileOutputStream out = context.openFileOutput(configPath,Context.MODE_PRIVATE);

参考Context.MODE_PRIVATE说明: http://www.cnblogs.com/yjpjy/p/5407251.html

完整代码

ProperTies 类
package com.**.demo.utils;

import android.content.Context;
import android.util.Log; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties; public class ProperTies {
//private static String configPath = getExternalStorageDirectory() + File.separator + "appConfig";
private static String configPath = "appConfig"; public static Properties getProperties(Context context) {
Log.e("configPath", configPath); Properties urlProps;
Properties props = new Properties();
try {
//方法一:通过activity中的context攻取setting.properties的FileInputStream
//注意这地方的参数appConfig在eclipse中应该是appConfig.properties才对,但在studio中不用写后缀
//InputStream in = c.getAssets().open("appConfig.properties"); //props.load(context.getAssets().open(configName)); //方法二:通过class获取setting.properties的FileInputStream
//InputStream in = PropertiesUtill.class.getResourceAsStream("/assets/ setting.properties ")); // 方法三
props.load(context.openFileInput(configPath));
// props.load(new FileInputStream(configPath)); } catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} urlProps = props;
return urlProps;
} //保存配置文件
public static String setProperties(Context context, String keyName, String keyValue) {
Properties props = new Properties();
try {
props.load(context.openFileInput(configPath));
props.setProperty(keyName, keyValue);
// FileOutputStream out = context.getAssets().openFd(configPath).createOutputStream();
FileOutputStream out = context.openFileOutput(configPath,Context.MODE_PRIVATE);
// FileOutputStream out = new FileOutputStream(configPath);
props.store(out, null); } catch (Exception e) {
e.printStackTrace();
Log.e("setPropertiesError", e.toString());
return "修改配置文件失败!";
}
return "设置成功";
} }
UrlString类:
package com.**.demo.json;

import android.content.Context;
import com.**.demo.utils.ProperTies; import java.util.Properties; /**
* 读写配置属性类
*/ public class UrlString { private String contrastIPName = "contrastIP"; // 上传路径
private String ip;
private String ipAddress; public void setIPAddress(Context context) {
Properties proper = ProperTies.getProperties(context);
this.ip = proper.getProperty(contrastIPName, "");
this.ipAddress = "http://" + this.ip + "/index.html";
} public String setIPAddress(Context context, String keyValue) {
String result = ProperTies.setProperties(context, contrastIPName, keyValue);
this.ip = keyValue;
this.ipAddress = "http://" + this.ip + "/index.html";
return result;
} public String getIP() {
return this.ip;
} public String getIPAddress() {
return this.ipAddress;
}
}

在activity中使用:

加载配置文件:

private UrlString urlString = new UrlString();

editText = (EditText) findViewById(R.id.et_ip);
// 加载配置的信息 --- IP地址
urlString.setIPAddress(this);
editText.setText(urlString.getIP()); // 获取完整地址
// urlString.getIPAddress()
 

修改配置文件:

String value = editText.getText().toString();
String result = urlString.setIPAddress(this,value); tools.customToast(result, ConfigActivity.this);

Android读写properties配置文件的更多相关文章

  1. java 顺序 读写 Properties 配置文件

    java 顺序 读写 Properties 配置文件 支持中文 不乱码 java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不 ...

  2. 【转】Java 读写Properties配置文件

    [转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...

  3. Java 读写Properties配置文件

    Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...

  4. java 顺序 读写 Properties 配置文件 支持中文 不乱码

    java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不是顺序读写的. 特从网上查资料,顺序读写的代码,如下, import j ...

  5. (转)Java 读写Properties配置文件

    原文:http://www.cnblogs.com/xudong-bupt/p/3758136.html 1.Properties类与Properties配置文件 Properties类继承自Hash ...

  6. java读写properties配置文件方法

    1.Properties类 Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载,属性列表中的key和value必须是字符串. 虽然Properties类继承了j ...

  7. Java 读写Properties配置文件【转】

    1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集.不过Properties有特殊的地 ...

  8. Properties类读写.properties配置文件

    package com.hzk.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFo ...

  9. java读写properties配置文件不改变属性的顺序和注释

    先贴代码 import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java ...

随机推荐

  1. macOS 上编译 Dynamips

    Dynamips 是一个Cisco 路由器模拟软件. 安装过程: git clone git://github.com/GNS3/dynamips.git cd dynamips mkdir buil ...

  2. luogu4770 [NOI2018]你的名字 (SAM+主席树)

    对S建SAM,拿着T在上面跑 跑的时候不仅无法转移要跳parent,转移过去不在范围内也要跳parent(注意因为范围和长度有关,跳的时候应该把长度一点一点地缩) 这样就能得到对于T的每个前缀,它最长 ...

  3. hive笔记

    cast cast(number as string),  可以将整数转成字符串 lpad  rpad lpad(target, 10, '0')   表示在target字符串前面补0,构成一个长度为 ...

  4. mybatis的if判断integer

    昨天在使用mybatis的if判断integer时遇见一个小问题: <if test="isChoose != null and isChoose != '' and isChoose ...

  5. javascript节点移除

    var itemdel = document.getElementById("test"); itemdel.removeChild(lis[0]); 兼容性较好 itemdel. ...

  6. [物理学与PDEs]第3章第1节 等离子体

    1.  磁流体力学研究等离子体这种导电流体在电磁场中的运动. 2.  任何物质由于 $T, p$ 等条件的不同而可以处于固态.液态.气态 (常见的三种聚集态) 或等离子体. 3.  等离子体就是电离气 ...

  7. 【搞事情】VS2015下的openGL初始化

    环境:glfw+glew+visual studio 2015 原材料下载链接: glfw 下载 glew 下载 glm库 下载 cmake 下载 (我下载的时候有些官网戳不开(大概校园网问题)... ...

  8. java8 list和map的forEach

    list forEach示例 public class HelloWorld { public static void main(String[] args) { List<User> l ...

  9. SpringMVC核心类和注解

    springMVC最重要的就是前端控制器DispatchServlet了.他是整个springMVC应用的核心. 需要将它配置在web.xml中. 1.DispatchServlet的配置 <! ...

  10. 2019最新迅为-i.MX6Q开发板资料目录

            迅为IMX6开发板: Android4.4系统  Linux + Qt5.7系统  Ubuntu12.04系统 部分案例:HMI:3D打印机:医疗设备:工控机:触控一体机:车载终端 核 ...