您的当前位置:首页正文

Android读写properties配置文件

2024-12-01 来源:个人技术集锦

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

配置文件的创建:

New --- File

命名后选择properties方式打开

配置文件设置

contrastIP = 192.166.1.65:8011

assets目录创建

New --- Folder --- Assets Folder

权限配置

在 AndroidManifest.xml 中添加:

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

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

先读配置:

Properties props = new Properties();
props.load(context.getAssets().open(configName));
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说明: 

 

完整代码

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);

 

显示全文