读取txt文件并在Android中输出为TextView

时间:2015-11-18 12:05:06

标签: android text ioexception

我正在尝试读取已保存在我的目录中的文本文件,并将其作为TextView打印在屏幕上。这是我到目前为止的代码。但是,当我运行应用程序时,它会创建一个“错误读取文件”的Toast。我在这做错了什么?

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);

}

}

enter image description here

4 个答案:

答案 0 :(得分:9)

如果要在项目中保留.txt文件,则必须将其放在assets文件夹中。
然后,您可以使用AssetManger访问它 阅读有关如何创建assets文件夹的this主题,然后使用此代码:

public class subActivity extends Activity {

private TextView textView;
private StringBuilder text = new StringBuilder();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text);
    BufferedReader reader = null;

    try {
        reader = new BufferedReader(
            new InputStreamReader(getAssets().open("inputNews.txt")));

        // do reading, usually loop until end of file reading  
        String mLine;
        while ((mLine = reader.readLine()) != null) {
            text.append(mLine);
            text.append('\n');
        }
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } finally {
        if (reader != null) {
        try {
            reader.close();
        } catch (IOException e) {
            //log the exception
        }
    }

    TextView output= (TextView) findViewById(R.id.summtext);
    output.setText((CharSequence) text);

 }
}

答案 1 :(得分:0)

据我所知,你无法从所谓的development folder读取文件。但是您可以将同一文件移动到development folder中的assets文件夹并从那里读取。即

try {
BufferedReader reader = new BufferedReader(
    new InputStreamReader(getAssets().open("inputNews.txt")));

StringBuilder text = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
         text.append(line);
         text.append('\n');
}   
} catch (IOException e) {
    Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
    e.printStackTrace();
}

我希望它有所帮助

答案 2 :(得分:0)

您应该将这些文件存储在assetsraw目录中。

之后,您可以使用

从这些文件中获取输入流

如果使用资产

AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");

如果你使用原始目录,那么

InputStream is = getResources().openRawResource(R.raw.test);

答案 3 :(得分:0)

这是上面答案的 Kotlin 版本:

var text = ""
var reader: BufferedReader? = null

try {
    reader = BufferedReader(InputStreamReader(assets.open("inputNews.txt")))
    text = reader.readLines().joinToString("\n")
} catch (e: IOException) {
    Toast.makeText(applicationContext, "Error reading license file!", Toast.LENGTH_SHORT).show()
    e.printStackTrace()
} finally {
    try {
        reader?.close()
    } catch (e: IOException) {
        //log the exception
        e.printStackTrace()
    }
    textView.text = text
}