• 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流都未关闭,在实际应用中记得关闭流。

  • 相关阅读:
    cordova build android get Execution failed for task ':dexArmv7Debug'
    brew install memcache get Error: Formulae found in multiple taps
    快速激活JetBrains系列产品
    NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
    `libsass` bindings not found. Try reinstalling `node-sass`?
    Cordova 快速入门记录
    perl: warning: Setting locale failed.
    httpclient源码分析之 PoolingHttpClientConnectionManager 获取连接
    httpclient源码分析之MainClientExec
    fastjson从1.1.41升级到1.2.28的坑
  • 原文地址:https://www.cnblogs.com/drizzlewithwind/p/5722129.html
Copyright © 2020-2023  润新知