zoukankan      html  css  js  c++  java
  • Properties工具类


    /*
    * Author: frank
    * Properties工具类
    */
    package com.he;


    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.Writer;
    import java.util.Properties;

    public class PropertiesTool {
        private static Properties prop = null;
        private static final String path = "src/test.properties" ;
            
        private static void init() {
            prop = new Properties();
            InputStream is = null;
            try {
                is = PropertiesTool.class.getClassLoader().getResourceAsStream("test.properties");
                prop.load(is);
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        
        public static String getProperty(String key) {
            
            if(prop == null) {
                init();
            }
            return prop.getProperty(key);
        }
        
         public static boolean setProperties(String key,String value) {  
               
               if(prop == null) {
                    init();
                }

                Writer writer = null;
                try {
                    writer = new FileWriter(path);
                    prop.setProperty(key, value);
                    prop.store(writer, key);  
                    
                    return true;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                } finally {
                    try {
                        if(writer != null) {
                            writer.close();
                        }
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                    
                }
         }  
    }

  • 相关阅读:
    java文件配置MySQL
    logback.xml
    Apache Commons之commons-lang
    Codec入门
    Mysql命令增加、修改、删除表字段
    markDown的简单使用
    Vert.x核心包各功能模块详解
    StringJoiner使用详解
    Vert.x初体验
    Docker镜像备份与迁移
  • 原文地址:https://www.cnblogs.com/hzm112567/p/2838426.html
Copyright © 2011-2022 走看看