Skip to content

Instantly share code, notes, and snippets.

@fuka
Last active September 26, 2017 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuka/6698e21f30aa6c9acb3f2cc2566cf03a to your computer and use it in GitHub Desktop.
Save fuka/6698e21f30aa6c9acb3f2cc2566cf03a to your computer and use it in GitHub Desktop.
assetsにあるテキストファイルを読み込むロジック。全部まとめてメモリに読み込むので長大なテキストには向いていないことに注意。
/**
* @param context Context
* @param fileName ファイル名
* @param charset 文字コード
* @return 読み込んだテキスト
*/
public static String loadStringFromAssets(Context context, String fileName, String charset) throws IOException {
String str = null;
InputStream is = null;
try {
is = context.getAssets().open(fileName);
byte[] buffer = new byte[is.available()];
is.read(buffer);
str = new String(buffer, charset);
} finally {
closeQuietly(is);
}
return str;
}
private static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception ignore) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment