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

我正在尝试读取已保存在我的目录中的文本文件,并将其作为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); } } 

在这里输入图像说明

如果你想在你的项目中保留.txt文件,你必须把它放在assest文件夹中。
然后您可以使用AssetManger访问它。
阅读这个主题来创建您的assest文件夹,然后使用下面的代码:

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

据我所知,你不能从所谓的development folder读取文件。 但是,您可以将相同的文件移动到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(); } 

我希望它有帮助

您应该将这些文件存储在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 }