Android最简单之读取文件内容

public List<String> Txt() {
        //将读出来的一行行数据使用List存储
        String filePath ="文件地址";
        List newList=new ArrayList<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 正则表达式
                        newList.add(count, reds);
                        count++;
                    }
                }
                isr.close();
                br.close();
            }else {
                Log.e("tag", "can not find file");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newList;
    }

猜你喜欢

转载自blog.csdn.net/weixin_42744183/article/details/89917819