Android读写properties配置文件

2023-05-16

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

配置文件读取很容易,修改需要注意权限,比如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.lemon.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.lemon.demo.json;

import android.content.Context;
import com.lemon.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);  

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • RedHat 6.5(x86_64)启动nagios客户端nrpe报错的解决方法

    tar xvf nagios tar gz C usr local usr local nagios bin nrpe c usr local nagios etc nrpe cfg d bash usr local nagios bin
  • wifi dhcp linux,archlinux 安装前的网络设置 静态IP DHCP 无线WIFI

    安装版本archlinux 20200701 xff0c 在安装前的网络配置 一 准备阶段 xff0c 查看网卡状态是否up xff0c 设置网卡为up状态 查看网卡信息 ip link 如果要使用的网卡包含state down字段 xff
  • java 判断 string null_java 字符串为null 如何判断

    判别一个字符串str不为空的办法有 xff1a 1 str 61 61 null 2 str isEmpty str 61 61 null 是有必要存在的 假如 String 类型为null 而去停止 equals String 或 len
  • What is my IP?

    今天介绍2个小工具 放心 xff0c 都是绿色的 xff0c 而且免安装 第一个叫whatismyip com 正如其名 xff0c 这个工具是用来看自己的IP的 啥 xff1f 你觉得太胡扯 xff1f 你是不是觉得看自己IP地址太简单
  • libqxt编译

    一 说明 编译环境 xff1a win10 qt5 6 1 1 vs2013和libqxt源码 从git上下载 libqxt xff1a libqxt 关于libqxt的说明 xff0c 请到libqxt的官网阅读 xff0c 说着看图1
  • ASP.NET CORE系列【五】webapi整理以及RESTful风格化

    原文 ASP NET CORE系列 五 webapi整理以及RESTful风格化 介绍 什么是RESTful xff1f 这里不多做赘述 xff0c 详情请百度 xff01 哈哈 xff0c 本来还想巴拉巴拉介绍一些webapi RESTf
  • mac系统如何生成SSH key与GitHub通信

    一 检查 SSH key 是否存在 在终端输入 xff1a ls al ssh 如果没有 xff0c 终端显示如下 xff1a No such file or directory 如果已经存在 xff0c 则会显示 id rsa 和 id
  • TortoiseSVN 忽略文件 忽略已提交文件

    主要以下两种情况 xff1a 1 首次提交就做好了忽略拦截 xff1a 项目首次提交到svn服务器的时候 xff0c 把该删的删了 xff0c 然后设置忽略规则 xff0c 就没问题了 2 提交一段时间忽然想忽略拦截 xff1a 经常碰到的
  • java里getter和setter的作用和区别是什么?

    java是典型的面向对象的编程语言 xff0c 面向对象三个特性 xff0c 继承性 xff0c 多态性 xff0c 封装性 xff0c 主要和封装性考虑 xff0c 类里面的变量不想设置成公共的类型 xff0c 但是还要给外部使用在这种实
  • FC金手指使用方法+大全

    一 文章来由 童年 小时候除了小霸王FC主机 xff0c 然后就是世嘉MD主机 xff0c 玩的好多啊 xff0c 但有些游戏一直没打穿留下遗憾 网上找金手指使用方法 xff0c 都真真假假 xff0c 鱼龙混杂 xff0c 试了很多终于得
  • shell根据关键字获取文件某一行的行号

    为什么80 的码农都做不了架构师 xff1f gt gt gt cat n 文件名 grep 39 关键字 39 awk 39 print 1 39 cat n是获取行号 xff0c 要是获取行内容 xff0c 去掉 n就可以了 转载于 h
  • VS Code编译支持C++11问题

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 如果你正确配置了 xff0c 能正确编译c 43 43 xff0c 但是发现auto等一些关键词不能使用 xff0c 那么 xff0c 请尝试如下操作 xff1a 打开ta
  • word2007自动生成参考文献引用并且右上角标注

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 在写毕业论文时 xff0c 总要处理四五十篇的参考文献的引用 xff0c 本文就介绍如何快捷自动生成参考文献引用 xff0c 同时实现参考文献右上角标注 打开需要排版的论文
  • matlab练习程序(随机粒子切换特效)

    视频制作软件中一般都会有相邻帧切换的特效 xff0c 我过去用过vagas好像就有很多切换特效 我想这个也算是其中一种吧 xff0c 虽然我不确定实际中到底有没有这种切换 实际上我只是下班后太无聊了 xff0c 写着玩的 xff0c 没什么
  • PyQt4(简单信号槽)

    import sys from PyQt4 import QtCore QtGui class myWidget QtGui QWidget def init self super myWidget self init self setWi
  • 模拟京东商城登陆HttpRequest

    利用Winform HttpRequest 模拟登陆京东商城 目前只获取订单信息 xff0c 可以获取图片等其他信息 1 using System 2 using System Collections Generic 3 using Sys
  • Nginx (一)Windows下编译Nginx源码以及安装 nginx for windows方法步骤

    转载自 http apps hi baidu com share detail 11192699 content Nginx介绍 xff1a Nginx 34 engine x 34 是一个高性能的 HTTP 和反向代理服务器 xff0c
  • 我所理解的人工智能

    很多人容易把人工智能理解为机器人 机器人是人工智能的一个实际体现 人工智能应用很广泛 下面我来谈谈我的理解 人工智能可分开理解为 人工 和 智能 xff0c 即人类创造出来的智能 xff0c 从广义上来讲只要人类创造出来 xff0c 能为人
  • [Oracle数据库] 存储过程出错 :PLS-00103: 出现符号 "("在需要下列之一时: := . ) , @...

    讨论原因之一 xff1a 我写的简单存储过程如下 xff1a create or replace procedure p c v date in varchar2 200 is t count number begin select cou
  • Android读写properties配置文件

    写这篇文章之前可以成功运行 文章后就各种找不到文件 所以并没有采用此种方式 后期完善 详见下篇解决方案 配置文件读取很容易 修改需要注意权限 比如assets目录下就不允许修改 配置文件的创建 New File 命名后选择propertie