`
mickey_hou
  • 浏览: 246196 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

PropertiesOperation

阅读更多
public class PropertiesOperation
{
   
    /**
     * 通过key  获取对应的value(url)
     *
     * @param String key[对应的索引]
     * @return String value[返回key所对应的value]
     * @throws
     * @since TianTian
     */
    public static String getValueByKeyFromProperties(String path,String key)
    {
        String value = null;
        InputStream inputStream = null;
        try
        {
            Properties properties = new Properties();
            inputStream = PropertiesOperation.class.getResourceAsStream(path);
            properties.load(inputStream);
            value = properties.getProperty(key);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (null != inputStream)
                {
                    inputStream.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return value;
    }
   
    /*
     * 写入properties信息
     */
    public static void writeProperties(Context context,String path,Map<String, String> parameters)
    {
        Properties props = new Properties();
        try {
            props.load(context.openFileInput(path));
            OutputStream out = context.openFileOutput(path,Context.MODE_PRIVATE);
//            Enumeration<?> e = props.propertyNames();
//            if(e.hasMoreElements())
//            {
//                while (e.hasMoreElements())
//                {
//                    String s = (String) e.nextElement();
//                    if (!s.equals(keyName))
//                    {
//                        props.setProperty(s, props.getProperty(s));
//                    }
//                }
//            }
            Set<String> keys = parameters.keySet();
            for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();)
            {
                String key = (String) iterator.next();
                props.setProperty(key, parameters.get(key));
               
                /*
                 *  更新原值
                 */
                props.store(out, "Update '" + key + "' value");
            }
//            props.setProperty(keyName, keyValue);
//            props.store(out, null);
//            String value = props.getProperty(keyName);
//            System.out.println(keyName + " "+value);
        }
        catch (FileNotFoundException e) {
            Log.v("system", "location.properties Not Found Exception");
        }
        catch (IOException e) {
            Log.v("system", "location.properties IO Exception");
        }
    }
   
    /**
     * 写入properties信息
     */
    public static void writeProperties(String filePath, Map<String, String> parameters)
    {
        Properties prop = new Properties();
        try
        {
            InputStream fis = new FileInputStream(filePath);
            prop.load(fis);
            OutputStream fos = new FileOutputStream(filePath);
            Set<String> keys = parameters.keySet();
            for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();)
            {
                String key = (String) iterator.next();
                prop.setProperty(key, parameters.get(key));
               
                /*
                 *  更新原值
                 */
                prop.store(fos, "Update '" + key + "' value");
            }
        }
        catch (IOException e)
        {
            System.out.println("Visit " + filePath + " for updating value error");
            e.printStackTrace();
        }
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics