Чтение текстового файла и вывод в виде TextView в Android
Я пытаюсь прочитать текстовый файл, который уже сохранен в моем каталоге, и распечатать его на экране в виде TextView. Это код, который у меня есть до сих пор. Однако, когда я запускаю приложение, оно создает тост с надписью «Ошибка чтения файла». Что я здесь не так делаю?
public class sub extends Activity {
private TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
//text = (TextView) findViewById(R.id.summtext);
//File file = new File("inputNews.txt");
//StringBuilder text = new StringBuilder();
try {
InputStream in = openFileInput("inputNews.txt");
if(in != null){
InputStreamReader reader = new InputStreamReader(in);
BufferedReader br = new BufferedReader(reader);
StringBuilder text = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
in.close();
}
}
catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
TextView output= (TextView) findViewById(R.id.summtext);
output.setText((CharSequence) text);
}
}