Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android 读取txt,按行读取的实例讲解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

一个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 读取txt,按行读取的实例讲解

以上这篇Android 读取txt,按行读取的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的Android 读取txt,按行读取的实例讲解全部内容,希望文章能够帮你解决Android 读取txt,按行读取的实例讲解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:txt读取