How do I load a resource and use its contents as a string in Spring

前端 未结 5 1030
春和景丽
春和景丽 2020-12-04 17:48

How can I load a Spring resource contents and use it to set a bean property or pass it as an argument constructor?

The resource contains free text.

5条回答
  •  难免孤独
    2020-12-04 18:17

    Just read it :

        try {
            Resource resource = new ClassPathResource(fileLocationInClasspath);
            BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()),1024);
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line).append('\n');
            }
            br.close();
            return stringBuilder.toString();
        } catch (Exception e) {
            LOGGER.error(e);
        }
    

提交回复
热议问题