zoukankan      html  css  js  c++  java
  • IO-文件 File 复制 读写 总结

    一定要注意:
    传入的参数,应该是包含文件名的完整路径名,不能把一个文件复制到【文件夹】中,因为【文件夹】本身是不能有输入输出流的,只能复制到一个【文件】中,否则会报异常。

    以字节流读写的三种方式
    public class Test {
        private static final String FILE_PATH = "e:\";
        private static final String FILE_TYPE = ".exe";
        private static final String FILE_FROM = FILE_PATH + 0 + FILE_TYPE;
        private static final String COPY_FILE_1 = FILE_PATH + 1 + FILE_TYPE;
        private static final String COPY_FILE_2 = FILE_PATH + 2 + FILE_TYPE;
        private static final String COPY_FILE_3 = FILE_PATH + 3 + FILE_TYPE;
        public static void main(String[] args) throws IOException {
            copyByBufStream(FILE_FROMCOPY_FILE_1);
            copyByBufArray(FILE_FROMCOPY_FILE_2);
            copyByByte(FILE_FROMCOPY_FILE_3);
        }
        /**
         * 利用缓冲输入流读取到一个缓冲容器后再写入。建议使用
         */
        public static boolean copyByBufStream(String filePathFrom, String filePathTo) {
            try {
                BufferedInputStream bufis = new BufferedInputStream(new FileInputStream(filePathFrom));
                BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(filePathTo));
                int ch = 0;
                while ((ch = bufis.read()) != -1) {
                    bufos.write(ch);
                }
                bufos.close();
                bufis.close();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        /**
         * 每次读取一个指定长度数组的字节。建议使用
         */
        public static boolean copyByBufArray(String filePathFrom, String filePathTo) {
            try {
                FileInputStream fis = new FileInputStream(filePathFrom);
                FileOutputStream fos = new FileOutputStream(filePathTo);
                byte[] buf = new byte[1024];
                int len = 0;
                while ((len = fis.read(buf)) != -1) {
                    fos.write(buf, 0, len);
                }
                fos.close();
                fis.close();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        /**
         * 一次读取一个字节。千万不要用,速度超级慢!
         */
        public static boolean copyByByte(String filePathFrom, String filePathTo) {
            try {
                FileInputStream fis = new FileInputStream(filePathFrom);
                FileOutputStream fos = new FileOutputStream(filePathTo);
                int ch = 0;
                while ((ch = fis.read()) != -1) {
                    fos.write(ch);
                }
                fos.close();
                fis.close();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
    }

    以字符流读写的三种方式
    public class Test {
        private static final String FILE_PATH = "e:\";
        private static final String FILE_TYPE = ".txt";
        private static final String FILE_FROM = FILE_PATH + 0 + FILE_TYPE;
        private static final String COPY_FILE_1 = FILE_PATH + 1 + FILE_TYPE;
        private static final String COPY_FILE_2 = FILE_PATH + 2 + FILE_TYPE;
        private static final String COPY_FILE_3 = FILE_PATH + 3 + FILE_TYPE;
        public static void main(String[] args) throws IOException {
            //注意,只能复制纯文本格式的文件,否则就会出现乱码
            copyByBufLine(FILE_FROMCOPY_FILE_1);
            copyByBufArray(FILE_FROMCOPY_FILE_2);
            copyByChar(FILE_FROMCOPY_FILE_3);
        }
        /**
         * 一次写入一行字符
         */
        public static boolean copyByBufLine(String filePathFrom, String filePathTo) {
            try {
                BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));
                BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));
                String line = null;
                //另外开辟一个缓冲区,存储读取的一行数据,返回包含该行内容的字符串,不包含换行符,如果已到达流末尾,则返回【 null】
                while ((line = bufr.readLine()) != null) {
                    bufw.write(line);
                    bufw.newLine();// 写入一个行分隔符
                    bufw.flush();
                }
                bufr.close();
                bufw.close();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        /**
         * 一次写入指定个数的字符
         */
        public static boolean copyByBufArray(String filePathFrom, String filePathTo) {
            try {
                BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));
                BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));
                char[] buf = new char[1024];
                int len = 0;
                while ((len = bufr.read(buf)) != -1) {
                    bufw.write(buf, 0, len);
                    bufw.flush();
                    len = 0;
                }
                bufr.close();
                bufw.close();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        /**
         * 一次写入一个字符
         */
        public static boolean copyByChar(String filePathFrom, String filePathTo) {
            try {
                BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));
                BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));
                int ch = 0;
                while ((ch = bufr.read()) != -1) {
                    bufw.write(ch);//写入单个字符
                }
                bufr.close();
                bufw.close();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
    }





  • 相关阅读:
    如何写好电子邮件
    【SQL查询】正则表达式匹配字符串_regexp_like/substr/instr/replace
    python_安装第三方库
    【SQL查询】分区查询Over
    【SQL查询】树结构查询
    【PL/SQL编程】块结构
    STL初探
    普及C组第四题(8.18)
    来纪中的的第18天
    来纪中的第十七天
  • 原文地址:https://www.cnblogs.com/baiqiantao/p/5587607.html
Copyright © 2011-2022 走看看