采用不同的方式,合并多个文件为一个文件。其中包括:Java方法,Windows脚本,CMD命令

2024-01-09 18:27:56

1. 批处理命令

可以实现不同文件的合并,将文件拖入这个命令即可。在这里插入图片描述

 @echo off
setlocal enabledelayedexpansion

set "outputFile=merged_output.txt"

rem Check if the output file already exists and delete it
if exist "%outputFile%" del "%outputFile%"

rem Loop through all the dragged files
for %%I in (%*) do (
    type "%%I" >> "%outputFile%"
)

echo Files merged successfully into %outputFile%
pause

在这里插入图片描述

2.合并.sql结尾的文件

```java
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;


public class MergeSqlFiles {

    public static void main(String[] args) {
        // 输入SQL文件夹路径
        String sqlFolder = "originalPath";
        // 合并后的文件路径和名称
        String mergedFilePath = "targetPath\\merged.sql";
        //方法1
        mergeSqlFiles(sqlFolder, mergedFilePath);
        //方法2
        mergeSqlFilesByStream(sqlFolder, mergedFilePath);
    }

    /**
     * 合并多个sql文件为一个sql文件
     *
     * @param folderPath
     * @param mergedFilePath
     */
    public static void mergeSqlFiles(String folderPath, String mergedFilePath) {
        //获取文件夹
        File floder = new File(folderPath);
        File[] files = floder.listFiles();
        //新的文件夹
        File newFile = new File(mergedFilePath);
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(newFile))) {
            if (files != null) {
                for (File file : files) {
                    if (file.isFile() && file.getName().endsWith(".sql") && !file.getName().startsWith("merged")) {
                        //读取文件内的内容
                        System.out.println("merging file: " + file.getName());
                        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
                            String line;
                            while ((line = bufferedReader.readLine()) != null) {
                                //写入新的一行
                                bufferedWriter.write(line);
                                //写入换行符号
                                bufferedWriter.newLine();
                            }
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void mergeSqlFilesByStream(String folderPath, String mergedFilePath) {
        //获取文件夹
        Path floder = Paths.get(folderPath);
        //新的文件夹
        Path mergeFile = Paths.get(mergedFilePath);
        try (OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(mergeFile))) {
            if (Files.isDirectory(floder)) {
                Files.list(floder)
                        .filter(file -> file.toFile().isFile() && file.getFileName().toString().endsWith(".sql") && !file.getFileName().toString().startsWith("merge"))
                        .forEach(file -> {
                            System.out.println("mergeFile:   " + file.getFileName());
                            try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(file))) {
                                byte[] bytes = new byte[1024];
                                int bytesRead;
                                while ((bytesRead=inputStream.read(bytes))!=-1){
                                    outputStream.write(bytes,0,bytesRead);
                                }
                                outputStream.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                        });
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

3.cmd命令

进入对应的文件目录使用此命令

copy *.sql merge.sql

文章来源:https://blog.csdn.net/weixin_44749255/article/details/135485187
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。