I would like to read the content of a file located in the Assets as a String. For example, a text document located in src/main/assets/
Original
hi this is in my opinion the cleanest approach:
public static String loadTextFromAssets(Context context, String assetsPath, Charset charset) throws IOException {
InputStream is = context.getResources().getAssets().open(assetsPath);
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int length = is.read(buffer); length != -1; length = is.read(buffer)) {
baos.write(buffer, 0, length);
}
is.close();
baos.close();
return charset == null ? new String(baos.toByteArray()) : new String(baos.toByteArray(), charset);
}
because readers could get trouble with line breaks.