Android 对.properties文件的读取

Posted 紫风幻雪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 对.properties文件的读取相关的知识,希望对你有一定的参考价值。


    /**
     * 
     * @param filepath .properties文件的位置
     */
    public  void checkFileExists(String filepath)
        File file = new File(filepath);
        if (file.exists()) 
            String s = PropertiesUtil.readValue(filepath, "allTime");
            if (s!=null) 
                ShowAllTime = Integer.parseInt(s)*60*1000;
            
            String mqtt = PropertiesUtil.readValue(filepath, "showTime");
            if (mqtt!=null) 
                ShowTime = Integer.parseInt(mqtt)*1000;
            
        
    

 

/**
 * 对Properties文件的操作
 * <p/>
 * 写入
 * PropertiesUtil mProp = PropertiesUtil.getInstance(this).init();
 * mProp.writeString("name", "Mr Lee");
 * mProp.commit();
 * 读取EG
 * PropertiesUtil mProp = PropertiesUtil.getInstance(this).init();
 * mProp.open();
 * String name = mProp.readString("name", "");
 *
 * @author lei
 */
public class PropertiesUtil 
    private Context mContext;
    private String mPath;
    private String mFile;
    private Properties mProp;
    private static PropertiesUtil mPropUtil = null;

    public static PropertiesUtil getInstance(Context context) 
        if (mPropUtil == null) 
            mPropUtil = new PropertiesUtil();
            mPropUtil.mContext = context;
            mPropUtil.mPath = Environment.getExternalStorageDirectory() + "/ExmKeyValue";
            mPropUtil.mFile = "properties.ini";
        
        return mPropUtil;
    

    public PropertiesUtil setPath(String path) 
        mPath = path;
        return this;
    

    public PropertiesUtil setFile(String file) 
        mFile = file;
        return this;
    

    public PropertiesUtil init() 
        try 
            File dir = new File(mPath);
            if (!dir.exists()) 
                dir.mkdirs();
            
            File file = new File(dir, mFile);
            if (!file.exists()) 
                file.createNewFile();
            
            InputStream is = new FileInputStream(file);
            mProp = new Properties();
            mProp.load(is);
            is.close();
         catch (Exception e) 
            e.printStackTrace();
        
        return this;
    

    public void commit() 
        try 
            File file = new File(mPath + "/" + mFile);
            OutputStream os = new FileOutputStream(file);
            mProp.store(os, "");
            os.close();
         catch (Exception e) 
            e.printStackTrace();
        
        mProp.clear();
    

    public void clear() 
        mProp.clear();
    

    public void open() 
        mProp.clear();
        try 
            File file = new File(mPath + "/" + mFile);
            InputStream is = new FileInputStream(file);
            mProp = new Properties();
            mProp.load(is);
            is.close();
         catch (Exception e) 
            e.printStackTrace();
        
    

    public void writeString(String name, String value) 
        mProp.setProperty(name, value);
    

    public String readString(String name, String defaultValue) 
        return mProp.getProperty(name, defaultValue);
    

    public void writeInt(String name, int value) 
        mProp.setProperty(name, "" + value);
    

    public int readInt(String name, int defaultValue) 
        return Integer.parseInt(mProp.getProperty(name, "" + defaultValue));
    

    public void writeBoolean(String name, boolean value) 
        mProp.setProperty(name, "" + value);
    

    public boolean readBoolean(String name, boolean defaultValue) 
        return Boolean.parseBoolean(mProp.getProperty(name, "" + defaultValue));
    

    public void writeDouble(String name, double value) 
        mProp.setProperty(name, "" + value);
    

    public double readDouble(String name, double defaultValue) 
        return Double.parseDouble(mProp.getProperty(name, "" + defaultValue));
    
    /**
     * 根据key读取value
     *
     * @param filePath
     * @param key
     * @return
     */
    public static String readValue(String filePath, String key) 
        Properties props = new Properties();
        try 
            InputStream in = new BufferedInputStream(new FileInputStream(
                    filePath));
            props.load(in);
            String value = props.getProperty(key);
            if (value.equals("")) 
                return null;
             else 
                return value;
            
         catch (Exception e) 
            e.printStackTrace();
            return null;
        
    

 

以上是关于Android 对.properties文件的读取的主要内容,如果未能解决你的问题,请参考以下文章

CMake,JNI 提升读取 json 文件 - Android

对Java配置文件Properties的读取写入与更新操作

Android读写properties配置文件

spring使用@Value注解读取.properties文件时出现中文乱码问题的解决

读取properties文件并获取属性值

struts2对properties资源的处理