原创

Java-小工具-大文件Sql分割小文件-navicat导入大sql文件中止现象说明

package cn.jiangjiesheng.utils.file;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.;*
import java.text.DecimalFormat;

public class FileSplitter {

    public static void main(String[] args) {

        //补充说明:
        //  navicat导入4g左右的sql可能会突然中止,但是又没有错误,很可能是格式的问题,使用如下的方法转成utf-8无bom格式后尝试再导入下。

        //非utf8格式的会有报错 java.nio.charset.MalformedInputException: Input length = 1
        //使用Uedit32 转换格式,没有明显的按钮,采用另存为,格式选择utf8-无bom
        String inputFilePath = "C:/Users/LP_T_24/Desktop/t_water_md_run_log_sub_utf8.sql";
        //String inputFilePath = "C:/Users/LP_T_24/Desktop/二维分组前的备份.txt";
        //String inputFilePath = "C:/Users/LP_T_24/Desktop/t_water_md_run_log_main.sql";

        String outputDirectory = "C:/Users/LP_T_24/Desktop/directory/";
        long maxFileSize = 1024L * 1024 * 1024; // 1GB in bytes ,实际出来1.3G,如果转成utf8-无bom格式,基本是对的
        //long maxFileSize = (long) (1024 * 1 /1.3 * 1024 * 1024); // 1GB in bytes ,实际出来1.3G

        splitFileBySize(inputFilePath, outputDirectory, maxFileSize);
    }

    private static void splitFileBySize(String inputFilePath, String outputDirectory, long maxFileSize) {
        try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputFilePath), StandardCharsets.UTF_8)) {
            String line;
            long currentFileSize = 0;
            int fileCounter = 0;
            BufferedWriter writer = null;
            int lineSeparatorLength = getLineSeparator().length();

            while ((line = reader.readLine()) != null) {
                if (writer == null || currentFileSize + line.length() + lineSeparatorLength > maxFileSize) { // +1 for newline character
                    double mbSize = currentFileSize / 1024f / 1024;
                    DecimalFormat df = new DecimalFormat("#.##");
                    String formattedNumber = df.format(mbSize);
                    System.out.println("当前文件写入前的大小: " + formattedNumber + "mb");
                    if (writer != null) {
                        writer.close();
                    }
                    if (!new File(outputDirectory).exists()) {
                        new File(outputDirectory).mkdirs();
                    }
                    String fileName = (outputDirectory + "/" + inputFilePath.substring(inputFilePath.lastIndexOf("/"), inputFilePath.lastIndexOf(".")) + "_part_" + fileCounter++ + "." + inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1))
                            .replaceAll("///", "/");
                    File file = new File(fileName);
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    writer = Files.newBufferedWriter(Paths.get(fileName));
                    currentFileSize = 0;
                }

                writer.write(line);
                writer.newLine();
                currentFileSize += line.length() + lineSeparatorLength; // +1 for newline character
            }

            if (writer != null) {
                writer.close(); // Close the last file
            }
        } catch (IOException e) {
            System.err.println("Error splitting file: " + e.getCause());
        }

    }

    private static String getLineSeparator() {
        return java.security.AccessController.doPrivileged(
                new sun.security.action.GetPropertyAction("line.separator"));
    }
}
正文到此结束
本文目录