java获取配置文件 所有数据

1. 第一种

直接放回

      Resource resource = new ClassPathResource("application.yml");
       byte[] bdata = FileCopyUtils.copyToByteArray(resource.getStream());
       String data = new String(bdata, StandardCharsets.UTF_8);
       System.out.println(data);
       return data;

2. 第二种

自己拼接换行,放回

      InputStream stream = resource.getStream();
       InputStreamReader reader = new InputStreamReader(stream);
       BufferedReader bufferedReader = new BufferedReader(reader);
       StringBuffer sb = new StringBuffer();
       String data = null;
       while ((data = bufferedReader.readLine()) != null){
           sb.append(data+ "\n");
           System.out.println(data + "\n");
       }
       bufferedReader.close();
       reader.close();
       stream.close();

       return sb.toString();

你可能感兴趣的:(springboot,接口,基础,java)