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");
     }

     
  • 相关阅读:
    tomact与eclipces的配置
    IDEA中Flink环境pom.xml文件配置
    python读写mysql
    用pyspark实现Wordcount
    python计算相似度
    Spark读取mysql
    Spark之WordCount
    IDEA中Spark环境pom.xml文件配置
    Spark之HelloWorld
    (转)二叉树的中序遍历与后序遍历构造二叉树思路(递归方法,含图片说明)
  • 原文地址:https://www.cnblogs.com/IceBlueBrother/p/8423019.html
Copyright © 2011-2022 走看看