2
1

More than 1 year has passed since last update.

【Android】assetsフォルダにあるテキストファイルを読み込む方法

Posted at
fun readFile(fileName: String): String {
    var text = ""

    try {
        val inputStream = context.assets.open(fileName)
        text = inputStream.bufferedReader().use {
            it.readText()
        }
    } catch (e: Exception) {
        // エラー処理
    }
    return text
}

val inputStream = context.assets.open(fileName)でassetsフォルダにあるfileNameファイルを読み込みます。
fileNameには拡張子を含んだ名前を渡します。
textには読み込んだテキストファイルの中身(改行も含む)が入ります。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1