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

     
  • 相关阅读:
    Tensorflow实现LSTM识别MINIST
    linux误删除恢复
    python使用工具简介介绍
    一个画ROC曲线的封装包
    Anaconda基本使用
    对于进程没杀死占用内存和cpu行为的方法
    Gluon
    原博客地址
    训练词向量
    TPU尝试
  • 原文地址:https://www.cnblogs.com/IceBlueBrother/p/8423019.html
Copyright © 2011-2022 走看看