Varias formas de leer archivos en Java

1. Leer un archivo usando una secuencia

public static void stream() {
    String fileName = "D:\\test.txt";
    final String CHARSET_NAME = "UTF-8";

    List<String> content = new ArrayList<>();

    try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), CHARSET_NAME))) {
        String line;
        while ((line = br.readLine()) != null) {
            content.add(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

//        content.forEach(System.out::println);
    System.out.println(content.size());
}

2. Utilice NIO proporcionado por JDK1.7 para leer archivos (adecuado para archivos pequeños)

public static void nioOfJDK7() {
    String fileName = "D:\\test.txt";
    final String CHARSET_NAME = "UTF-8";

    List<String> content = new ArrayList<>(0);

    try {
        content = Files.readAllLines(Paths.get(fileName), Charset.forName(CHARSET_NAME));
    } catch (Exception e) {
        e.printStackTrace();
    }

//        content.forEach(System.out::println);
    System.out.println(content.size());
}

3. Utilice NIO proporcionado por JDK1.7 para leer archivos (adecuado para archivos grandes)

public static void streamOfJDK7() {
    String fileName = "D:\\test.txt";
    final String CHARSET_NAME = "UTF-8";

    List<String> content = new ArrayList<>();

    try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName), Charset.forName(CHARSET_NAME))) {
        String line;
        while ((line = br.readLine()) != null) {
            content.add(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

//        content.forEach(System.out::println);
    System.out.println(content.size());
}

4. Utilice NIO proporcionado por JDK1.4 para leer archivos (adecuado para archivos muy grandes)

public static void nioOfJDK4() {
    String fileName = "D:\\test.txt";
    final String CHARSET_NAME = "UTF-8";
    final int ASCII_LF = 10; // 换行符
    final int ASCII_CR = 13; // 回车符

    List<String> content = new ArrayList<>();

    try (FileChannel fileChannel = new RandomAccessFile(fileName, "r").getChannel()) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 100);
        byte[] lineByte;

        byte[] temp = new byte[0];

        while (fileChannel.read(byteBuffer) != -1) {
            // 获取缓冲区位置,即读取长度
            int readSize = byteBuffer.position();

            // 将读取位置置0,并将读取位置标为废弃
            byteBuffer.rewind();

            // 读取内容
            byte[] readByte = new byte[readSize];
            byteBuffer.get(readByte);

            // 清除缓存区
            byteBuffer.clear();

            // 读取内容是否包含一整行
            boolean hasLF = false;

            int startNum = 0;

            for (int i = 0; i < readSize; i++) {
                if (readByte[i] == ASCII_LF) {
                    hasLF = true;
                    int tempNum = temp.length;
                    int lineNum = i - startNum;

                    // 数组大小已经去掉换行符
                    lineByte = new byte[tempNum + lineNum];
                    System.arraycopy(temp, 0, lineByte, 0, tempNum);
                    temp = new byte[0];
                    System.arraycopy(readByte, startNum, lineByte, tempNum, lineNum);

                    String line = new String(lineByte, 0, lineByte.length, CHARSET_NAME);

                    content.add(line);

                    // 过滤回车符和换行符
                    if (i + 1 < readSize && readByte[i + 1] == ASCII_CR) {
                        startNum = i + 2;
                    } else {
                        startNum = i + 1;
                    }
                }
            }
            if (hasLF) {
                temp = new byte[readByte.length - startNum];
                System.arraycopy(readByte, startNum, temp, 0, temp.length);
            } else {
                // 单次读取的内容不足一行的情况
                byte[] toTemp = new byte[temp.length + readByte.length];
                System.arraycopy(temp, 0, toTemp, 0, temp.length);
                System.arraycopy(readByte, 0, toTemp, temp.length, readByte.length);
                temp = toTemp;
            }
        }

        // 最后一行
        if (temp.length > 0) {
            String lastLine = new String(temp, 0, temp.length, CHARSET_NAME);

            content.add(lastLine);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

//        content.forEach(System.out::println);
    System.out.println(content.size());

}

5. Utilice la clase de herramienta FileUtils proporcionada por cmmons-io para leer archivos

Agregar dependencias:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>
public static void fileOfCommonsIO() {
        String fileName = "D:\\test.txt";
        final String CHARSET_NAME = "UTF-8";

        List<String> content = new ArrayList<>(0);

        try {
            content = FileUtils.readLines(new File(fileName), CHARSET_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        content.forEach(System.out::println);
        System.out.println(content.size());
    }

6. Utilice la clase de herramienta IOtils proporcionada por cmmons-io para leer archivos

Agregar dependencias:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>
public static void ioOfCommonsIO() {
        String fileName = "D:\\test.txt";
        final String CHARSET_NAME = "UTF-8";

        List<String> content = new ArrayList<>(0);

        try {
            content = IOUtils.readLines(new FileInputStream(fileName), CHARSET_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        content.forEach(System.out::println);
        System.out.println(content.size());
    }

7. Utilice la clase de herramienta FileUtil proporcionada por hutool para leer archivos

Agregar dependencias:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-core</artifactId>
    <version>5.8.10</version>
</dependency>

或者:
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.10</version>
</dependency>
public static void fileOfHutool() {
        String fileName = "D:\\test.txt";
        final String CHARSET_NAME = "UTF-8";

        List<String> content = FileUtil.readLines(fileName, CHARSET_NAME);

//        content.forEach(System.out::println);
        System.out.println(content.size());
    }

8. Utilice la clase de herramienta IoUtil proporcionada por hutool para leer archivos

Agregar dependencias:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-core</artifactId>
    <version>5.8.10</version>
</dependency>

或者:
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.10</version>
</dependency>
public static void ioOfHutool() {
        String fileName = "D:\\test.txt";
        final String CHARSET_NAME = "UTF-8";

        List<String> content = new ArrayList<>();

        try {
            IoUtil.readLines(new FileInputStream(fileName), CharsetUtil.charset(CHARSET_NAME), content);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        content.forEach(System.out::println);
        System.out.println(content.size());
    }

9. Pruebas que requieren mucho tiempo

  Archivo de prueba: 30000 líneas, 21,8 MB

public static void main(String[] args) {
    StopWatch stopWatch = new StopWatch();

    stopWatch.start("stream");
    stream();
    stopWatch.stop();

    stopWatch.start("nioOfJDK7");
    nioOfJDK7();
    stopWatch.stop();

    stopWatch.start("streamOfJDK7");
    streamOfJDK7();
    stopWatch.stop();

    stopWatch.start("nioOfJDK4");
    nioOfJDK4();
    stopWatch.stop();

    stopWatch.start("fileOfCommonsIO");
    fileOfCommonsIO();
    stopWatch.stop();

    stopWatch.start("ioOfCommonsIO");
    ioOfCommonsIO();
    stopWatch.stop();

    stopWatch.start("fileOfHutool");
    fileOfHutool();
    stopWatch.stop();

    stopWatch.start("ioOfHutool");
    ioOfHutool();
    stopWatch.stop();

    for (StopWatch.TaskInfo taskInfo : stopWatch.getTaskInfo()) {
        System.out.println(taskInfo.getTaskName() + " -> " + taskInfo.getTimeMillis() + " ms");
    }

    
//    System.out.println(stopWatch.prettyPrint());
}

  Pruebe 3 estadísticas que requieren mucho tiempo (unidad: ms):

Número de serie de prueba arroyo nioOfJDK7 corrienteDeJDK7 nioOfJDK4 archivoOfCommonsIO ioOfCommonsIO archivoDeHutool ioOfHutool
1 110 113 85 214 109 64 178 60
2 98 126 77 236 135 70 169 59
3 106 122 90 224 130 68 165 62

  Según los resultados de la prueba, IoUtil proporcionado por Hutool, IoUtil proporcionado por commons-io y NIO proporcionado por JDK1.7 consumen más tiempo según el método de flujo, pero la prueba también debe referirse al uso de memoria, y usted puede probar hágalo usted mismo.

Supongo que te gusta

Origin blog.csdn.net/qq_48008521/article/details/129312430
Recomendado
Clasificación