用IO流进行文件读写的封装工具类

第一次发博客,谢谢支持~~~

import android.os.Environment;


import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/28.
 */

public class FileUtils {
    //获取sd卡绝对路径
    private static String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    public static File file = new File(path, "up.txt");
    public static String sp = "------";

    public static void writeup(String str, boolean b) {

        if (!file.isDirectory()) {
            //内存输入字节流
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes());
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(file, b);
                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = byteArrayInputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, len);
                }
                byteArrayInputStream.close();
                fileOutputStream.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 一个蠢萌蠢萌的读取文本的方法
     */
    public static List<String> readstr(File file) {
        ArrayList<String> strings = new ArrayList<>();
        if (file.exists() && !file.isDirectory()) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
//                int len = 0;
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
                String read = "";
                while ((read = bufferedReader.readLine()) != null) {
                    strings.add(read);
                }
                bufferedReader.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return strings;
    }

    /***
     * 分割的方法
     */
    public static String[] getstr(String str) {
        //123213
        String[] strings = new String[2];
        if (str.length() > sp.length() + 1) {
            int i = str.indexOf("-");
            if (i != -1) {
                String substring = str.substring(0, i);
                strings[0] = substring;
            }
            int i1 = str.lastIndexOf("-");
            if (i1 != -1) {
                String substring = str.substring(i1 + 1, str.length());
                strings[1] = substring;
            }
        }
        return strings;
    }

}

指导老师:张志伟


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值