从文本文件读出的数据和在TextView的显示它 [英] reading data from a textfile and displaying it on the textview
问题描述
我试图读取一个文本TEMP.TXT,这是在我的原始文件夹中的数据和文本视图文本显示文件的内容,只要一个按钮的按钮被点击,但我的应用程序崩溃而这样做,有相当多的可能性,我这样做了错误的方式,因为我是新来的Android和Java编程。我在这里粘贴code,任何帮助将AP preciated
i am trying to read data from a textfile "temp.txt" which is in my raw folder and displaying the contents of the file on the text view "text" whenever a button "button" is clicked, but my app crashes while doing so, there is quite a possibility that i am doing it in a wrong way because i am new to android and java programming. i am pasting the code here, any help will be appreciated
情况下R.id.b:
InputStream is = getResources().openRawResource(R.raw.temp);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
string = br.readLine();
while(string != null){
st = string;
}
text.setText(st);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
ST和串都是字符串变量。我会很高兴,如果任何人都可以在其他简单的方法指出,做同样的。
"st" and "string" are both string variables. i will be glad if anyone can point out at another simple method to do the same.
推荐答案
更改为以下内容:
InputStream is = getResources().openRawResource(R.raw.temp);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
String entireFile = "";
try {
while((line = br.readLine()) != null) { // <--------- place readLine() inside loop
entireFile += (line + "\n"); // <---------- add each line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text.setText(entireFile); // <------- assign entireFile to TextView
break;
这篇关于从文本文件读出的数据和在TextView的显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!