cscib 548890541c Bael 2434 (#6635)
* BAEL-2434

* BAEL-2434

* Minor alterations to reflect changes in the article

* Fixing Spel expression and removing extra code that is not referenced anymore in the article

* Moved the files from spring-core to spring-static-resources. Renamed ResourceUtil to ResourceReader.

* Changes due to change of article outline.

* Changes due to change of article outline.

* Removing Converter from article
2019-06-18 21:35:04 +01:00

31 lines
983 B
Java

package com.baeldung.loadresourceasstring;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.FileCopyUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import static java.nio.charset.StandardCharsets.UTF_8;
public class ResourceReader {
public static String readFileToString(String path) {
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource(path);
return asString(resource);
}
public static String asString(Resource resource) {
try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) {
return FileCopyUtils.copyToString(reader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}