In the spring-boot project in the resources folder there is an xml template. Only one value changes in it. How can I get the contents of a file as a string with the embedded value of $ value in the code.

<body> <phone>$value</phone> </body> 

Now just zhardkodil part of the xml to the value and after and concatenated string.

    1 answer 1

    To read a file from a resource, you can write the following method:

     private String readResource(String name) { ClassLoader classloader = Thread.currentThread().getContextClassLoader(); try ( InputStream inputStream = classloader.getResourceAsStream(name); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)) ) { return reader.lines().collect(Collectors.joining("\n")); } catch (Exception e) { return null; } } 

    And after reading, replace $value via replace :

     readResource("file.xml").replace("$value", "{some_value}");