zoukankan      html  css  js  c++  java
  • Properties

    package resource.and.property;
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Enumeration;
    import java.util.Properties;
    
    public class Main {
        public static final String THIS_PATH = Main.class.getResource("").getPath();
        public static final String CONFIG_FILE = THIS_PATH + "app.properties";
        public static final InputStream INPUT = Main.class
                .getResourceAsStream("app.properties");
    
        public static void main(String[] args) {
            // getJVMProperties();
            getProperties();
        }
    
    
        public static void getJVMProperties() {
            Properties props = System.getProperties();
            props.list(System.out);
        }
    
        public static void getProperties() {
            Properties pps = new Properties();
            try {
                pps.load(new FileInputStream(CONFIG_FILE));
                // pps.load(INPUT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            @SuppressWarnings("rawtypes")
            Enumeration enum1 = pps.propertyNames();
            while (enum1.hasMoreElements()) {
                String strKey = (String) enum1.nextElement();
                String strValue = pps.getProperty(strKey);
                System.out.println(strKey + "=" + strValue);
            }
        }
    
        static class TestProperties {
            public static String GetValueByKey(String filePath, String key) {
                Properties pps = new Properties();
                try {
                    InputStream in = new BufferedInputStream(new FileInputStream(
                            filePath));
                    pps.load(in);
                    String value = pps.getProperty(key);
                    System.out.println(key + " = " + value);
                    return value;
    
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
    
            @SuppressWarnings("rawtypes")
            public static void GetAllProperties(String filePath) throws IOException {
                Properties pps = new Properties();
                InputStream in = new BufferedInputStream(new FileInputStream(
                        filePath));
                pps.load(in);
                Enumeration en = pps.propertyNames();
    
                while (en.hasMoreElements()) {
                    String strKey = (String) en.nextElement();
                    String strValue = pps.getProperty(strKey);
                    System.out.println(strKey + "=" + strValue);
                }
    
            }
    
            public static void WriteProperties(String filePath, String pKey,
                    String pValue) throws IOException {
                Properties pps = new Properties();
                InputStream in = new FileInputStream(filePath);
                pps.load(in);
                OutputStream out = new FileOutputStream(filePath);
                pps.setProperty(pKey, pValue);
                pps.store(out, "Update " + pKey + " name");
            }
    
            public static void main(String[] args) throws IOException {
                String value = GetValueByKey(CONFIG_FILE, "name");
                System.out.println(value);
                System.out.println("--------------");
                GetAllProperties(CONFIG_FILE);
                System.out.println("--------------");
                WriteProperties(CONFIG_FILE, "long", "212");
                GetAllProperties(CONFIG_FILE);
            }
        }
    }

    一般两种方式获得properties:

    pps.load(new BufferedInputStream(new FileInputStream(filePath)));
    
    
    pps.load(Main.class.getResourceAsStream(filePath));

    注意:上述代码中各种stream流都未关闭,在实际应用中记得关闭流。

  • 相关阅读:
    Java构造方法之间的调用
    JavaNote
    微信小程序-智能机器人
    微信小程序-今日头条案例
    微信小程序-记账本
    51job爬虫
    Xcode文件目录选中变成白色, 解决方案
    Mac通过以太网共享网络
    Mac系统Safari浏览器启动无图模式
    iOS9.0之后不支持http请求解决方案
  • 原文地址:https://www.cnblogs.com/drizzlewithwind/p/5722129.html
Copyright © 2011-2022 走看看