android读取多行文件,Android 读取txt,按行读取的实例讲解

Android 读取txt,按行读取的实例讲解

发布时间:2020-09-12 12:43:31

来源:脚本之家

阅读:103

作者:Damionew

一个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 Txt() {

//将读出来的一行行数据使用Map存储

String filePath = "/sdcard/sdl_log.txt";//手机上地址

Map map = new HashMap();

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;

}

}

06c5eb2f7de88ee382890f586082c287.png

ea4ca02de3b83facd2b253177124a6a4.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值