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

Reading a txt file and outputing as a TextView in Android

我正在尝试读取已保存在我的目录中的文本文件并将其作为 TextView 打印在屏幕上。这是我到目前为止的代码。但是,当我 运行 应用程序时,它会创建一个显示 "Error Reading File" 的 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);

}

}

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

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

希望对你有帮助

如果要在项目中保留 .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);

 }
}

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

之后,您可以使用 ,

从这些文件中获取输入流

如果使用资产

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

或者如果您使用原始目录,则

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

这是上面答案的 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
}