栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021-09-28

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-09-28

**

Andriond Studio APP 在手机读写文件**

注意点:需要在Activity中引用并 注入上下文

package com.xwsoft.zthx.driver.common.util;

import android.app.Application;
import android.content.Context;
import org.jsoup.helper.StringUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;


public class PropertiesUtil extends Application {

    
    public  String getPropertyByKey(String key) {
        Properties props = new Properties();
        try {
            String fileName = "face.properties";
            props.load(getApplicationContext().getAssets().open(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String value = props.getProperty(key.trim());
        if (StringUtil.isBlank(value)) {
            value = "defaultValue";
        }
        return value.trim();
    }



    //初始化配置文件
    //最好放在Application中,初始化。
    public  String initProperties() {
        Properties props = new Properties();
        try {
            String fileName = "face.properties";
            props.load(getApplicationContext().getAssets().open(fileName));
            props.load(getApplicationContext().getAssets().open("face.properties"));
            FileOutputStream out = getApplicationContext().openFileOutput("face.properties", Context.MODE_PRIVATE);
            props.store(out, null);
        } catch (Exception e) {
            e.printStackTrace();
            return "修改配置文件失败!";
        }
        return "设置成功";
    }


    
    public  void setProperty(String key,String value) {
        if(!value.isEmpty()){
            try {
                String path = "/assets/";
                String fileName = "face.properties";
                File file = new File(path, fileName);
                OutputStream fos = new FileOutputStream(file);
                Properties props = new Properties();
                props.setProperty(key,value);
                props.store(fos, "Update '" + key + "' value");
            } catch (IOException e) {
                System.err.println("属性文件更新错误");
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/273992.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号