Last active
September 26, 2017 12:25
-
-
Save fuka/6698e21f30aa6c9acb3f2cc2566cf03a to your computer and use it in GitHub Desktop.
assetsにあるテキストファイルを読み込むロジック。全部まとめてメモリに読み込むので長大なテキストには向いていないことに注意。
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
/** | |
* @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