java中读取配置文件的内容

深藏阁楼爱情的钟 2024-04-07 13:06 144阅读 0赞

说明:前三种是不被Spring管理的读取配置文件内容,后面是被Spring管理的读取配置文件内容

第一种:读取application.properties中的内容

第一种方式需要添加依赖

  1. <dependency>
  2. <groupId>com.typesafe</groupId>
  3. <artifactId>config</artifactId>
  4. <version>1.2.1</version>
  5. </dependency>
  6. public static void test01(){
  7. Config load = ConfigFactory.load("application.properties");
  8. String string = load.getString("ruoyi.name");
  9. System.out.println(load);
  10. System.out.println(string);
  11. }

第二种:读取application.yml中的内容

  1. public static void test02() throws IOException {
  2. Resource resource = new ClassPathResource("application.yml");
  3. Properties props = PropertiesLoaderUtils.loadProperties(resource);
  4. String property = props.getProperty("addressEnabled");
  5. System.out.println(property);
  6. }

第三种:以BufferedInputStream流的方式读取配置文件内容

  1. public static void test03() throws Exception{
  2. //2、通过InputStream进行读取文件
  3. //可以读取任意路径的文件
  4. Properties properties = new Properties();
  5. // 使用InPutStream流读取properties文件
  6. BufferedReader bufferedReader =
  7. new BufferedReader(new FileReader("E:/config.properties"));
  8. properties.load(bufferedReader);
  9. properties.getProperty("ruoyi.name");
  10. }

发表评论

表情:
评论列表 (有 0 条评论,144人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java按行读取文件内容

    java中按行来读取文件内容,一般对文件也是又要求的,比如文件编码utf-8,内容是按行可读,而不是一堆字节码。这类文件,我们按行读取,主要是方便快速查看内容,并且用这些内容来