Skip to content

Instantly share code, notes, and snippets.

@alxsimo
Created March 12, 2016 14:51
Show Gist options
  • Save alxsimo/ba63bccb43fdb462c597 to your computer and use it in GitHub Desktop.
Save alxsimo/ba63bccb43fdb462c597 to your computer and use it in GitHub Desktop.
[Android] Load resources from unit test
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
public class ResourceHelper {
public String convertFileToString(File file) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
public String getContentFromFile(String filePath) throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource(filePath);
File file = new File(resource.getPath());
return convertFileToString(file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment