• 【Properties】获取Properties文件


    获取Properties文件

    package com.chinamobile.epic.tako.v2.query.commons;
    
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.support.PropertiesLoaderUtils;
    
    import java.io.*;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Properties;
    import java.util.concurrent.ConcurrentHashMap;
    
    public class PropertiesUtilsBase {
    
        /**
         * 获取属性文件
         * @param path example: c:\my.properties
         * @return
         */
        public static Properties loadFromFileSystem(String path) {
            Properties props = new Properties();
            try {
                File file = new File(path);
                InputStream in = new BufferedInputStream(new FileInputStream(file));
    
                //解决中午乱码问题--因为字节流无法读取中文,所以采用reader把inputStream转换成reader用字符流来读取中文
                BufferedReader bf = new BufferedReader(new InputStreamReader(in));
                props.load(bf);
                in.close();
            } catch (Exception e) {
                return null;
            }
            return props;
        }
    
        /**
         * 从classpath中获取属性文件
         *
         * @param path graphite/graphiteTargets.properties
         * @return
         */
        public static Properties loadFromClassPath(String path) {
            Resource resource = new ClassPathResource(path);
            Properties prop = null;
            try {
                prop = PropertiesLoaderUtils.loadProperties(resource);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return prop;
        }
    
        public static Map<String, String> asMap(Properties properties) {
            if (properties == null) {
                return null;
            }
    
            Map<String, String> entryMap = new ConcurrentHashMap<String, String>();
    
            for (Map.Entry<Object, Object> entry : properties.entrySet()) {
                String key = entry.getKey().toString();
                String value = entry.getValue().toString();
                entryMap.put(key, value);
            }
            return entryMap;
        }
    
        /**
         * 显示所有key,value
         *
         * @param properties
         */
        public static void showKeysAndValues(Properties properties) {
            if (properties == null) {
                System.out.println("properties == null");
                return;
            }
            Iterator<Map.Entry<Object, Object>> it = properties.entrySet().iterator();
    
            System.out.println("======下面将显示所有<key,value>值---方式1============");
            while (it.hasNext()) {
                Map.Entry<Object, Object> entry = it.next();
                Object key = entry.getKey().toString();
                Object value = entry.getValue();
                System.out.println("<" + key + "," + value + ">");
            }
        }
    }
    
    

    使用@Bean方式获取Properties

       @Bean
        public Properties quartzProperties() throws IOException {
            PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
            propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
            propertiesFactoryBean.afterPropertiesSet();
            return propertiesFactoryBean.getObject();
        }
    
  • 相关阅读:
    小组开发地铁项目
    Qt 编译时遇到 error: [debug/qrc_music.cpp] Error 1
    Qt 使用irrlicht(鬼火)3D引擎
    Qt编译出错:During startup program exited with code 0xc0000135
    Qt 飞机仪表显示
    Qt 在Label上面绘制罗盘
    Qt 播放音频文件
    Git 使用 粗糙记录
    Qt 建立带有子项目的工程
    QSS 的选择器
  • 原文地址:https://www.cnblogs.com/ssslinppp/p/7224734.html
Copyright © 2020-2023  润新知