java - 如何在android中读取txt文件?
问题描述
package com.example.popword;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.File;
import java.util.Scanner;
public class ReadText1 extends Activity {
TextView textview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.textView);
try (Scanner scanner = new Scanner(new File("C:\\Users\\UOU\\Desktop\\word.txt"))) {
while (scanner.hasNext()) {
textview.setText(scanner.nextLine());
}
} catch (Exception e) {
System.err.println("Exception occurred!");
}
}
}
==================================================== ================================== 2019-11-13 20:07:44.224 1004-1004/com.example .popword E/cklee: com.example.popword.ReadText1.printStackTrace(ReadText1.java:29) com.example.popword.ReadText1.onCreate(ReadText1.java:22) android.app.Activity.performCreate(Activity.java:第7136章 android.app.Activity.performCreate(Activity.java:7127) android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) android.app.ActivityThread。 handleLaunchActivity(ActivityThread.java:3048) android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) android.os.Handler.dispatchMessage(Handler.java:106) android. os.Looper.loop(Looper.java:193) android.app.ActivityThread.main(ActivityThread.java:6669) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.RuntimeInit$MethodAndArgsCaller .run(RuntimeInit.java:493) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)app.ActivityThread.main(ActivityThread.java:6669) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) com.android.internal .os.ZygoteInit.main(ZygoteInit.java:858)app.ActivityThread.main(ActivityThread.java:6669) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) com.android.internal .os.ZygoteInit.main(ZygoteInit.java:858)
解决方案
您可以按照以下步骤直接从存储或 SD 卡中读取文件:
1.将这两个权限添加到你的项目清单文件中
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.创建以下函数
private void ReadTxtFile(File txtfilepath) {
if (txtfilepath.exists()) {
StringBuffer txt = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader(txtfilepath));
String line;
while ((line = br.readLine()) != null) {
txt.append(line);
txt.append('\n');
}
br.close();
} catch (IOException e) {
Log.e("Exception:",e.toString());
}
Log.e("File Text:",txt.toString());// "txt" variable contian your file text
}
}
随时随地调用函数并提供文本文件路径作为函数参数
ReadTxtFile(new File("/sdcard/Download/sample.txt"));
推荐阅读
- github-actions - 如果 PR 编号在数组中,则跳过作业
- c++ - float for 循环,核心转储 c++
- python - ValueError:发现样本数量不一致的输入变量:TimeSeries 中的 [367, 24]
- c# - 是否可以在 ML.Net 中使用保存为 ONNX 文件的自动编码器模型?
- excel - VBA 新手 - 似乎无法设置页脚
- r - 'lib is not writable error download package in R on remote server
- javascript - 如何在应用程序加载时在提供程序之外调度操作?
- c# - 有没有办法从 Azure 应用配置中获取所有功能及其布尔值?
- django-models - Django - 具有多个外键的模型
- debugging - 使用 Network Monitor (Firefox/Chrome) 调试或捕获 Next.js 网络请求很热门