zoukankan      html  css  js  c++  java
  • java 读写操作大文件 BufferedReader和RandomAccessFile

    老问这问题,两个都答出来算加分项?

    具体代码如下,没什么好说的直接说对比。

    BufferedReader和RandomAccessFile的区别
    RandomAccessFile 在数据越大,性能越差。因为他是数据文件的一个channel,支持读改原数据文件。

    BufferedReader是读改数据文件的一个在内存的副本。

    那RandomAccessFile的优点?

    1.RandomAccessFile忽略了字符编码的处理,加快了处理速度

    2.若是对数据操作在BufferedReader创立buffer的时候就做完了,RandomAccessFile自然就快了。

    ps.

    小文件RandomAccessFile,大文件BufferedReader

    按行生成文件和按大小生成文件都实现了

    BufferedReader提供处理字符编码的方式,使用InputStreamReader或者DataInputStream之类的。

        private static void fileRead() throws IOException {
            long time = System.currentTimeMillis();
    
            int bufSize = 10 * 1024 * 1024;
            byte[] bs = new byte[bufSize];
            ByteBuffer byteBuf = ByteBuffer.allocate(bufSize);
            FileChannel channel = new RandomAccessFile(input_path, "r").getChannel();
            FileWriter fw = null;
            for (int i = 0; channel.read(byteBuf) != -1; i++) {
                byteBuf.rewind();
                int size = byteBuf.limit();
                byteBuf.get(bs);
    
                fw = new FileWriter(String.format(output_path_format1, i));
                String line = new String(bs, 0, size);
                fw.append(line + System.getProperty("line.separator"));
                fw.flush();
                byteBuf.clear();
            }
            fw.close();
            time = System.currentTimeMillis() - time;
            System.out.println("file read time = " + time);
        }
    
        private static void bufferRead() throws IOException {
            long time = System.currentTimeMillis();
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(input_path)));
    
            int bufferSize = 10 * 1024 * 1024;
            BufferedReader in = new BufferedReader(new InputStreamReader(bis, "utf-8"), bufferSize);
            FileWriter fw = new FileWriter(String.format(output_path_format2, 0));
    
            for (int i = 0; in.ready(); i++) {
                if (i % 100 == 0) {
                    fw = new FileWriter(String.format(output_path_format2, i / 100));
                }
                String line = in.readLine();
                fw.append(line + System.getProperty("line.separator"));
                if (i % 100 == 0) {
                    fw.flush();
                }
            }
            in.close();
            fw.close();
            time = System.currentTimeMillis() - time;
            System.out.println("buffer read time = " + time);
        }

    这是生成大文件的代码,修改for循环次数控制文件大小,下面生成的文件大小是2G左右

    ps.

    操作文件时,尽量使用以下动态的的符号

    File.separator是分隔符不同系统是不一样的

    System.getProperty("line.separator")是换行符不同系统是不一样的

        private static String input_path = System.getProperty("user.dir") + File.separator + "data" + File.separator + "bigdata.txt";
        private static String output_path_format1 = System.getProperty("user.dir") + File.separator + "data" + File.separator + "part_1_%s.txt";
        private static String output_path_format2 = System.getProperty("user.dir") + File.separator + "data" + File.separator + "part_2_%s.txt";
        private static  int bufSize = 10 * 1024 * 1024;
    
         private static void makeBigData() throws IOException {
            FileWriter fw = new FileWriter(input_path);
            String line = "start ";
            for (int i = 0; i < 20000; i++) {
                line += i;
                fw.append(line + System.getProperty("line.separator"));
            }
            fw.flush();
            fw.close();
            System.out.println("end");
        }

    因为是自己琢磨的,总感觉写的有点丑,特别是生成大文件那里,希望各位指正一番。

    源码地址 https://github.com/247292980/spring-boot 。fork的比star还多什么道理啊。

  • 相关阅读:
    面试题系列---【字符串常用方法、数组常用方法、Number常用方法】
    面试题系列---【箭头函数和普通函数的区别?箭头函数的优缺点?】
    面试题系列---【什么是Promise,解决什么问题?Promise.all解决了什么问题?实现原理?回调地狱?】
    面试题系列---【data数据改变,页面不更新原因及解决方案】
    JavaScript课程——Day20(jQuery:使用、选择器、节点遍历操作、其他属性操作)
    JavaScript课程——Day18(本地存储、JSON方法)
    JavaScript课程——Day16(扩展原型上的方法、继承)
    JavaScript课程——Day15(编程思想、对象的读写、面向对象的创建、面向对象的案例)
    JavaScript课程——Day14(回调函数、自执行函数、闭包、递归、防抖与节流、call与apply)
    JavaScript课程——Day13(4、无缝轮播图)
  • 原文地址:https://www.cnblogs.com/ydymz/p/10141343.html
Copyright © 2011-2022 走看看