一个TXT 文件 对其进行读取,并且每行都单个存储读取
public class MainActivity extends AppCompatActivity {
private TextView textView,textView2,textView3;
private Button click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
String s1 = Txt().get(0);//从Map get (key = 0)的value
String s2 = Txt().get(1);
String s3 = Txt().get(2);
textView.setText(s1);
textView2.setText(s2);
textView3.setText(s3);
}
public Map<Integer, String Txt() {
//将读出来的一行行数据使用Map存储
String filePath = "/sdcard/sdl_log.txt";//手机上地址
Map<Integer, String map = new HashMap<Integer, String ();
try {
File file = new File(filePath);
int count = 0;//初始化 key值
if (file.isFile() && file.exists()) { //文件存在的前提
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
BufferedReader br = new BufferedReader(isr);
String lineTxt = null;
while ((lineTxt = br.readLine()) != null) { //
if (!"".equals(lineTxt)) {
String reds = lineTxt.split("\+")[0]; //java 正则表达式
map.put(count, reds);//依次放到map 0,value0;1,value2
count++;
}
}
isr.close();
br.close();
}else {
Toast.makeText(getApplicationContext(),"can not find file",Toast.LENGTH_SHORT).show();//找不到文件情况下
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
以上这篇Android 读取txt,按行读取的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考。
更多Android进阶指南 可以扫码 解锁 《Android十大板块文档》

1.Android车载应用开发系统学习指南(附项目实战)
2.Android Framework学习指南,助力成为系统级开发高手
3.2023最新Android中高级面试题汇总+解析,告别零offer
4.企业级Android音视频开发学习路线+项目实战(附源码)
5.Android Jetpack从入门到精通,构建高质量UI界面
6.Flutter技术解析与实战,跨平台首要之选
7.Kotlin从入门到实战,全方面提升架构基础
8.高级Android插件化与组件化(含实战教程和源码)
9.Android 性能优化实战+360°全方面性能调优
10.Android零基础入门到精通,高手进阶之路
敲代码不易,关注一下吧。ღ( ´・ᴗ・` ) 🤔