You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.2 KiB
37 lines
1.2 KiB
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
|
|
public class ResourceApi {
|
|
|
|
public String getFileFromResourceAsString(String fileName) throws IOException {
|
|
InputStream inputStream = getFileFromResourceAsStream(fileName);
|
|
return getStringFromInputStream(inputStream);
|
|
}
|
|
|
|
public InputStream getFileFromResourceAsStream(String fileName) {
|
|
//ClassLoader classLoader = getClass().getClassLoader();
|
|
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
|
InputStream inputStream = classLoader.getResourceAsStream(fileName);
|
|
|
|
if (inputStream == null) {
|
|
throw new IllegalArgumentException("file not found! " + fileName);
|
|
} else {
|
|
return inputStream;
|
|
}
|
|
}
|
|
|
|
public String getStringFromInputStream(InputStream inputStream) throws IOException {
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
|
|
String inputLine;
|
|
StringBuffer response = new StringBuffer();
|
|
|
|
while ((inputLine = in.readLine()) != null) {
|
|
response.append(inputLine + "\n");
|
|
}
|
|
in.close();
|
|
|
|
return response.toString();
|
|
}
|
|
}
|