Created
March 12, 2016 14:51
-
-
Save alxsimo/ba63bccb43fdb462c597 to your computer and use it in GitHub Desktop.
[Android] Load resources from unit test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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