java之IO

© 版权声明:本文为博主原创文章,转载请注明出处

1.RandomAccessFile:访问保存数据记录的文件的

  1.1 创建对象时必须指定对文件的操作方式。r:只读 rw:读写

  1.2 该对象读取文件时存在一个指针,指向当前位置;因此程序可以跳到任何地方读写数据

  1.3 raf.getFilePointer():定位,获取指针当前所在位置

  1.3 raf.seek(pos):跳转,将指针移动到文件中的指定位置

2.字节流

  2.1 InputStream:抽象类,不能直接生成对象,它是所有字节输入流的父类。输入流主要用于读操作。is.read()

  2.2 OutputStream:抽象类,不能直接生成对象,它是所有字节输出流的父类。输出流主要用于写操作。os.writer()

  2.3 FileInputStream:用于读取本地文件中的字节数据

  2.4 FileOutputStream:用于将字节数据写入到文件中

/**
     * 通过字节数组拷贝文件
     *
     * @param srcFile
     *                  源文件
     * @param destFile
     *                  目标文件
     * @throws IOException
     */
    public static void copyFileByByteArray(String srcFile, String destFile) throws IOException {

        long startTime = System.currentTimeMillis();// 开始时间

        // 将文件以字节流的形式进行读/写操作
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);

        byte[] buff = new byte[1024];// 存放一次读取的字节数据
        int b;// 一次性读取的字节个数

        // 批量读取数据
        while ((b = fis.read(buff, 0, buff.length)) != -1) {
            fos.write(buff, 0, b);// 批量写入数据
        }

        // 关闭文件输入/输出流
        fis.close();
        fos.close();

        long endTime = System.currentTimeMillis();// 结束时间
        System.out.println("copFileByByteArray执行时间为:" + (endTime - startTime) + "ms");

    }

  2.5 BufferedInputStream:带缓存区的输入流,默认缓存区大小是8M,可减少访问磁盘的次数,提高文件读取性能

  2.6 BufferedOutputStream:带缓存区的输出流,可以提高文件写入效率

/**
     * 通过字节数组缓存拷贝文件
     *
     * @param srcFile
     *                  源文件
     * @param destFile
     *                  目标文件
     * @throws IOException
     */
    public static void copyFileByByteArrayBuffer(String srcFile, String destFile) throws IOException {

        long startTime = System.currentTimeMillis();// 开始时间

        // 将文件以带缓存区的字节流的形式进行读/写操作
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

        byte[] buff = new byte[1024];// 定义缓存区
        int b;// 一次读取的字节个数

        // 批量读取字节数据并放入缓存区中
        while ((b = bis.read(buff, 0, buff.length)) != -1) {
            bos.write(buff, 0, b);// 批量将缓存区中的数据写入
            bos.flush();// 刷新缓冲区
        }

        // 关闭带缓存区的输入/输出流
        bos.close();
        bis.close();

        long endTime = System.currentTimeMillis();// 结束时间
        System.out.println("copFileByByteArrayBuffer执行时间为:" + (endTime - startTime) + "ms");

    }

3.字符流

  3.1 InputStreamReader:字节流与字符流之间的桥梁,将字节输入流以指定的编码格式转换为字符输入流

  3.2 OutputStreamWriter:字节流与字符流之间的桥梁,将字节输出流以指定的编码格式转换为字符输出流

/**
     * 通过字符流批量拷贝文件
     *
     * @param srcFile
     *                  源文件
     * @param destFile
     *                  目标文件
     * @throws IOException
     */
    public static void copyFileByIsrAndOsw(String srcFile, String destFile) throws IOException {

        long startTime = System.currentTimeMillis();// 开始时间

        // 创建文件输入流
        FileInputStream fis = new FileInputStream(srcFile);
        // 将字节流以UTF-8的格式转换为字符流
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        // 创建文件输出流
        FileOutputStream fos = new FileOutputStream(destFile);
        // 将字节流以UTF-8的格式转换为字符流
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

        char[] buff = new char[1024];
        int b;// 读取的字符数据
        while ((b = isr.read(buff, 0, buff.length)) != -1) {// 批量读取字符
            osw.write(buff, 0, b);// 输出字符
        }

        // 关闭字符输入流
        isr.close();
        // 关闭字符输出流
        osw.close();
        // 关闭字节输入流
        fis.close();
        // 关闭字节输出流
        fos.close();

        long endTime = System.currentTimeMillis();// 结束时间
        System.out.println("copyFileByByteIsrAndOsw执行时间为:" + (endTime - startTime) + "ms");

    }

  3.4 FileReader:以字符流的形式读取本地文件

  3.5 FileWriter:将字符流的形式将数据写入到文件中

/**
     * 通过字符流批量拷贝文件
     *
     * @param srcFile
     *                  源文件
     * @param destFile
     *                  目标文件
     * @throws IOException
     */
    public static void copyFileByFrAndFw(String srcFile, String destFile) throws IOException {

        long startTime = System.currentTimeMillis();// 开始时间

        FileReader fr = new FileReader(srcFile);
        FileWriter fw = new FileWriter(destFile);

        char[] buff = new char[1024];
        int b;// 读取的字符数据
        while ((b = fr.read(buff, 0, buff.length)) != -1) {// 批量读取字符
            fw.write(buff, 0, b);// 输出字符
        }

        // 关闭字符输入流
        fw.close();
        // 关闭字符输出流
        fr.close();

        long endTime = System.currentTimeMillis();// 结束时间
        System.out.println("copyFileByFrAndFw执行时间为:" + (endTime - startTime) + "ms");

    }

  3.6 BufferedReader:带缓存区的字符输入流

  3.7 BufferedWriter:带缓存区的字符输出流

/**
     * 通过字符流批量拷贝文件
     *
     * @param srcFile
     *                  源文件
     * @param destFile
     *                  目标文件
     * @throws IOException
     */
    public static void copyFileByBrAndBw(String srcFile, String destFile) throws IOException {

        long startTime = System.currentTimeMillis();// 开始时间

        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(srcFile)));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(destFile)));

        String line;// 读取的行数据
        while ((line = br.readLine()) != null) {// 批量读取字符
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        // 关闭字符输入流
        bw.close();
        // 关闭字符输出流
        br.close();

        long endTime = System.currentTimeMillis();// 结束时间
        System.out.println("copyFileByBrAndBw执行时间为:" + (endTime - startTime) + "ms");

    }

  

参考:http://www.imooc.com/learn/123

posted @ 2017-08-07 14:14  禁忌夜色153  阅读(211)  评论(0编辑  收藏  举报