Java简单文件处理

来源:互联网 发布:淘宝粤通卡车宝是什么 编辑:程序博客网 时间:2024/06/08 17:51
/** * 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
原创粉丝点击