Java简单文件处理

/**
 * Created by z5139 on 2016/5/17 0017.
 */
public class IOtxt {
    /**
     * 读取数据
     */
    public static String ReadData(String url) {
        String buffer = null;
        try {
            FileReader read = new FileReader(new File(url));
            StringBuffer sb = new StringBuffer();
            char ch[] = new char[1024];
            int d = read.read(ch);
            while (d != -1) {
                String str = new String(ch, 0, d);
                sb.append(str);
                d = read.read(ch);
            }
            buffer = sb.toString();
            read.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;

    }

    /**
     * 写入数据
     */
    public static boolean WriteData(String url, int content) {
        BufferedWriter out = null;
        boolean flag;
        try {
            File file = new File(url);
            if (!file.exists()) {
                file.createNewFile();
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true)));
            out.write(String.valueOf(content) + " ");
            flag = true;
            return flag;
        } catch (Exception ex) {
            System.out.println(ex);
            flag = false;
            return flag;
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 创建文件
     */
    public static boolean createFile(String url) throws Exception {
        boolean flag = false;
        File fileName = new File(url);
        try {
            if (!fileName.exists()) {
                fileName.createNewFile();
                flag = true;
            }
        } catch (Exception e) {
            flag = false;
            Log.e("tag", "创建失败");
        }
        return flag;
    }


    /**
     * String转 int 数组
     */
    public static int[] StringToInt(String a) {
        String str[] = a.trim().split("\\s");
        int recordInt[] = new int[str.length];
        for (int i = 0; i < str.length; i++) {
            recordInt[i] = Integer.parseInt(str[i]);
        }
        return recordInt;
    }
    /**
     * 列出指定文件夹内路径
     */
    public static String[] listFile(String path) {
        File file = new File(path);
        File[] tempList = file.listFiles();
        String filepath[] = new String[tempList.length];
        System.out.println("该目录下对象个数:" + tempList.length);
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                filepath[i] =  tempList[i].getName();
            }
        }
        return filepath;
    }
    /**
     * 列出指定文件夹内时间
     */
    public static String[] listTime(String path) {
        File file = new File(path);
        File[] tempList = file.listFiles();
        String time[] = new String[tempList.length];
        System.out.println("该目录下对象个数:" + tempList.length);
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                time[i] =  tempList[i].getName().substring(0, 20);
            }
        }
        return time;
    }
    /**
     * 列出指定文件夹内HR
     */
    public static String[] listHR(String path) {
        File file = new File(path);
        File[] tempList = file.listFiles();
        String HR[] = new String[tempList.length];
        System.out.println("该目录下对象个数:" + tempList.length);
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                HR[i] =  tempList[i].getName().substring(20,22);
            }
        }
        return HR;
    }
    //删除空白文件
    public static void deleteEmpty(String path){
        File file = new File(path);
        if (file.length() == 0){
            file.delete();
        }
    }
    //创建文件夹
        public static void makeRootDirectory(String filePath) {
        File file;
        try {
            file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
        } catch (Exception e) {
            Log.i("error:", e + "");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值