java 文件操作工具

文章目录

    • 1、文件切片
      • [1.1、File 切片成 N 个保存到本地](#1.1、File 切片成 N 个保存到本地)
      • [1.2、MultipartFile 切成 N 个 InputStream](#1.2、MultipartFile 切成 N 个 InputStream)
    • 2、目录中的文件读取

1、文件切片

1.1、File 切片成 N 个保存到本地

java 复制代码
    /**
     * 读取本地文件 sourceFile , 切成 chunkSize 个文件
     * @param sourceFile 源文件
     * @param splitFilePath 切片后要保存的目录
     * @param chunkSize 切片数量
     */
    public static void fileChunking(String sourceFile, String splitFilePath, int chunkSize) {
        try (FileInputStream fis = new FileInputStream(sourceFile)) {
            byte[] buffer = new byte[chunkSize];
            int bytesRead;
            int chunkNumber = 1;

            while ((bytesRead = fis.read(buffer)) > 0) {
                String chunkFileName = splitFilePath + "chunking" + chunkNumber + ".txt";
                try (FileOutputStream fos = new FileOutputStream(chunkFileName)) {
                    fos.write(buffer, 0, bytesRead);
                }
                chunkNumber++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

使用示例

java 复制代码
fileChunking("D:\\1.txt", "D:\\chunking", 3);

1.2、MultipartFile 切成 N 个 InputStream

将 前端输入进来的文件 分成N个输入流

java 复制代码
    /**
     * 将 前端输入进来的文件 分成N个输入流
     * @param file 前端输入进来的文件
     * @return 输出流List
     */
    public static List<InputStream> splitFile(MultipartFile file) {
        try {
            int chunkSize = 3; // 每个分片的大小,这里设置为1MB,可以根据需要进行调整
            byte[] fileBytes =  file.getBytes();
            int totalChunks = (int) Math.ceil((double) fileBytes.length / chunkSize);
            List<InputStream> chunkInputStreams = new ArrayList<>();

            for (int i = 0; i < totalChunks; i++) {
                int offset = i * chunkSize;
                int chunkLength = Math.min(chunkSize, fileBytes.length - offset);
                byte[] chunkBytes = new byte[chunkLength];
                System.arraycopy(fileBytes, offset, chunkBytes, 0, chunkLength);
                InputStream chunkInputStream = new ByteArrayInputStream(chunkBytes);
                chunkInputStreams.add(chunkInputStream);
            }
            return chunkInputStreams;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

// 使用示例:List<InputStream> inputStreams = splitFile(file);

将输出流Lits保存为多个文件

java 复制代码
    /**
     * 将多个输出流保存为多个文件
     * @param inputStreams 输出流List
     * @param outputDirectory 要保存文件的目录
     */
    public static void saveToMultipleFiles(List<InputStream> inputStreams, String outputDirectory) {
        File directory = new File(outputDirectory);
        if (!directory.exists()) {
            directory.mkdirs();
        }

        try {
            for (int i = 0; i < inputStreams.size(); i++) {
                String filePath = outputDirectory + File.separator + "file" + (i + 1) + ".txt";
                try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath))) {
                    InputStream inputStream = inputStreams.get(i);
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, bytesRead);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

// 使用示例: saveToMultipleFiles(inputStreams, "D:\\test\\");

2、目录中的文件读取

读取一个目录下所有txt后缀的文件,按文件内容输出为List<List>

java 复制代码
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class FileProcessor {
    public static void main(String[] args) throws IOException {
        String directoryPath = "/path/to/directory"; // 替换为实际的目录路径

        List<List<String>> fileContents = readTxtFiles(directoryPath);
        fileContents.forEach(System.out::println);
    }

    public static List<List<String>> readTxtFiles(String directoryPath) throws IOException {
        List<Path> txtFiles = Files.walk(Paths.get(directoryPath))
                .filter(Files::isRegularFile)
                .filter(path -> path.toString().endsWith(".txt"))
                .collect(Collectors.toList());

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

        for (Path txtFile : txtFiles) {
            List<String> lines = new ArrayList<>();

            try (BufferedReader reader = new BufferedReader(new FileReader(txtFile.toFile()))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    lines.add(line);
                }
            }

            fileContents.add(lines);
        }

        return fileContents;
    }
}
相关推荐
shepherd11130 分钟前
吞吐量提升 10 倍:高并发大批量数据处理任务的架构演进与性能调优
java·后端·架构
zzzzzz3103 小时前
当产品经理说这个很简单:我用Python自动化处理奇葩需求的实战指南
python·pycharm·产品经理
plainGeekDev3 小时前
单例模式 → object 声明
android·java·kotlin
雪隐4 小时前
个人电脑玩AI-06让5060 Ti给你打工——不光能画画,Qwen3-TTS还能学人说话,连我老板都信了!
人工智能·后端·python
用户298698530144 小时前
Java 实现 Word 文档文本与图片提取的方法
java·后端
SimonKing5 小时前
铁子,IntelliJ IDEA 2026.1.3来了,升不升?
java·后端·程序员
兵慌码乱15 小时前
面向桌面端的资产管理系统分层架构设计与核心模块实现
python·系统架构·sqlite·pyqt5·数据库设计·桌面应用开发·mvc架构
咖啡八杯16 小时前
GoF设计模式——策略模式
java·后端·spring·设计模式
hboot17 小时前
AI工程师第三课 - 机器学习基础
python·scikit-learn·kaggle