zoukankan      html  css  js  c++  java
  • 读取properties文件中的内容

     看情况有时候某些属性值在很多地方要用,而且可能会有改动,就可以把它们存在properties文件中。

     当然也可以准备一个静态类来放。

    //调用方法的静态代码块
    private static String getStr = "";
    static {
      try {
        Properties properties = PropertiesUtil.readProperties("sys.properties");
        //Str是文件中配置的名字部分
        //例:文件中存了几个属性,其中一个是Str=test
        //那么取出来的数据中,getStr=test(取出来的都是字符串)
        getStr = properties.getProperty("Str");
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

     

    //方法类
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class PropertiesUtil {
      public static Properties readProperties(String fileName) throws IOException {
        Properties properties = new Properties();
        InputStream inStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
        if (inStream == null) {
          throw new RuntimeException(fileName + " not found");
        }
    
        properties.load(inStream);
        return properties;
      }
    }


    方法二
    这个比上面的方法简单很多,我更喜欢用这个
    import java.util.ResourceBundle;

    private static final String Str;

     static{
      ResourceBundle rb = ResourceBundle.getBundle("sys");
      Str=rb.getString("Str");
     }

     
  • 相关阅读:
    通向全栈之路——(4)nginx反向代理配置
    通向全栈之路——(3)node环境搭建
    通向全栈之路——(2)服务器安全设置
    通向全栈之路——(1)服务器新增用户及授权
    window.location.href跳转至空白页
    webpack 1.x 学习总结
    win7下使用apache ab 比较测试node与 tomcat
    Enum,Int,String的互相转换
    读写appSettings配置节方法
    SC命令
  • 原文地址:https://www.cnblogs.com/IceBlueBrother/p/8423019.html
Copyright © 2011-2022 走看看