从textview查看txt文件

时间:2015-04-30 20:38:45

标签: java android

我想通过文本视图显示txt文件并使用此代码

TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
InputStream iFile = getResources().openRawResource(R.raw.quizhelp);
String strFile = inputStreamToString(iFile);
helpText.setText(strFile);

在api 8我测试过,这段代码工作正常,但在我的新项目中,我将min sdk设置为15,无法识别inputStreamToString方法!我应该使用什么方法呢? tnx

2 个答案:

答案 0 :(得分:1)

您可以使用BufferedReader

TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
InputStream iFile = getResources().openRawResource(R.raw.quizhelp);

BufferedReader bufferReader = new BufferedReader(new InputStreamReader(iFile));
StringBuilder sb = new StringBuilder();
String newline = System.getProperty("line.separator");
String line;

try {
    while ((line = bufferReader.readLine()) != null) {
        sb.append(line);
        sb.append(newline);
    }
} catch (IOException e) {
    e.printStackTrace();
}

helpText.setText(sb.toString());

答案 1 :(得分:0)

你可以这样做

TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
try {
    Resources res = getResources();
    InputStream ins = res.openRawResource(R.raw.quizhelp);

    byte[] b = new byte[ins.available()];
    ins.read(b);
    helpText.setText(new String(b));
} catch (Exception e) {
    e.printStackTrace();
}