zoukankan      html  css  js  c++  java
  • java使用java.util.Properties读取properties文件的九种方法

    直接上代码:

    package com.test.test;
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.util.Enumeration;
    import java.util.Locale;
    import java.util.Properties;
    import java.util.PropertyResourceBundle;
    import java.util.ResourceBundle;
    
    import org.springframework.core.io.support.PropertiesLoaderUtils;
    
    public class TestProperties {
        private static TestProperties testProperties = new TestProperties();
        public static void main(String[] args) {
            //获取properties配置文件中的值
            Properties prop = new Properties();
            try {
                prop.load(test1());//包含2种方法
                prop.load(test2());//包含2种方法
                prop.load(testProperties.test3());//包含2种方法
                //使用spring-core包封装好的方法
                prop = PropertiesLoaderUtils.loadAllProperties("test.properties");
                Enumeration<?> e = prop.propertyNames();
                while (e.hasMoreElements()) {
                    String key = (String) e.nextElement();
                    System.out.println(key+"="+new String(prop.getProperty(key).getBytes("ISO-8859-1"),"UTF-8"));
                }
                test4();
                test5();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        /**
         * 使用FileInputStream文件输入流
         * @return
         */
        public static InputStream test1(){
            InputStream in = null;
            try {
                //此处是相对于项目的相对路径
                //in = new FileInputStream("src/main/resources/test.properties");
                //
                in = new BufferedInputStream(new FileInputStream("src/main/resources/test.properties"));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return in;
        }
        /**
         * 使用ClassLoader
         * 默认从classPath路径下找文件
         * @return
         */
        public static InputStream test2(){
            //InputStream in = ClassLoader.getSystemResourceAsStream ("test.properties");
            //
            InputStream in = testProperties.getClass().getClassLoader().getResourceAsStream("test.properties");
            return in;
        }
        /**
         * 使用class变量的getResourceAsStream()方法
         * 文件名前不加“/”,则表示从当前类所在的包下查找该资源
         * 文件名前加了“/”,则表示从classPath路径下查找资源
         * @return
         */
        public InputStream test3(){
            //InputStream in = getClass().getResourceAsStream("/test.properties");
            //
            InputStream in = TestProperties.class.getResourceAsStream("/test.properties");
            return in;
        }
        /**
         * 使用java.util.ResourceBundle类的getBundle()方法
         * Locale.getDefault():没有提供语言和地区的资源文件是系统默认的资源文件
         * test:不需要文件的后缀
         */
        public static void test4(){
            try {
                ResourceBundle rb = ResourceBundle.getBundle("test", Locale.getDefault());
                Enumeration<String> e1 = rb.getKeys();
                while (e1.hasMoreElements()) {
                    String key = e1.nextElement();
                    System.out.println(key+"="+new String(rb.getString(key).getBytes("ISO-8859-1"),"UTF-8"));
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        /**
         * 使用java.util.PropertyResourceBundle类的构造函数
         */
        public static void test5(){
            InputStream in = ClassLoader.getSystemResourceAsStream ("test.properties");
            try {
                ResourceBundle rb = new PropertyResourceBundle(in);
                Enumeration<String> e1 = rb.getKeys();
                while (e1.hasMoreElements()) {
                    String key = e1.nextElement();
                    System.out.println(key+"="+new String(rb.getString(key).getBytes("ISO-8859-1"),"UTF-8"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
        
    }

    test.properties文件中的内容是:

    name=天若有情
    password=天亦老

    运行程序后控制台输出test.properties文件中的内容。

  • 相关阅读:
    C#调用C++代码遇到的问题总结
    ASP.NET MVC Controller的激活
    利用fis3自动化处理asp.net项目静态资源时遇到的一个编码问题
    工作中遇到的一个多线程下导致RCW无法释放的问题
    在mongoose中使用$match对id失效的解决方法
    passport源码研究
    android手机旋转屏幕时让GridView的列数与列宽度自适应
    git 版本回退
    梦之解读:如何成为牛人
    DataTable与DTO对象的简易转换类
  • 原文地址:https://www.cnblogs.com/aqsunkai/p/6690587.html
Copyright © 2011-2022 走看看