こちらの記事を参考に以下のコードを作成しました。
「1行ずつ読み込み、改行を付加する」という部分の仕組みがわかりません。
また、1行ずつではなく、特定の文字を区切りとすることは可能でしょうか。
なお、質問のレベルも踏まえたうえで初心者にもわかりやすい回答をお願いできればと思います。
Java
1public class MainActivity extends AppCompatActivity { 2 3 private TextView textView; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 textView = (TextView)findViewById(R.id.textView); 10 11 InputStream is = null; 12 BufferedReader br = null; 13 String text = ""; 14 15 try { 16 try { 17 // assetsフォルダ内の sample.txt をオープンする 18 is = this.getAssets().open("sample.txt"); 19 br = new BufferedReader(new InputStreamReader(is)); 20 21 // 1行ずつ読み込み、改行を付加する 22 String str; 23 while ((str = br.readLine()) != null) { 24 text += str + "\n"; 25 } 26 } finally { 27 if (is != null) is.close(); 28 if (br != null) br.close(); 29 } 30 } catch (Exception e){ 31 // エラー発生時の処理 32 } 33 34 // 読み込んだ文字列を EditText に設定し、画面に表示する 35 textView.setText(text); 36 } 37} 38
あなたの回答
tips
プレビュー