国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 讀取txt,按行讀取的實例講解

2019-12-12 00:56:55
字體:
來源:轉載
供稿:網友

一個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("http://+")[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,按行讀取的實例講解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庐江县| 乐业县| 高陵县| 平乡县| 葵青区| 巴马| 布拖县| 团风县| 资中县| 武安市| 师宗县| 五原县| 西乡县| 巴马| 昌图县| 大冶市| 新乡县| 南召县| 乌苏市| 阿图什市| 阳曲县| 呼图壁县| 肃宁县| 锦屏县| 延津县| 郯城县| 阜新市| 桂阳县| 鲜城| 临沭县| 武汉市| 高密市| 清苑县| 平乡县| 合作市| 资源县| 星子县| 乐亭县| 浏阳市| 田东县| 白玉县|