java文件合并帮助诶
package com.longgeutil; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.springframework.util.CollectionUtils; import lombok.extern.slf4j.Slf4j; /** * * @author roger yang * @date 11/04/2019 */ @Slf4j public class FileUtils { public static boolean mergeFiles(List<String> fpaths, String resultPath) { if (CollectionUtils.isEmpty(fpaths) || StringUtils.isEmpty(resultPath)) { return false; } if (fpaths.size() == 1) { return new File(fpaths.get(0)).renameTo(new File(resultPath)); } File[] files = new File[fpaths.size()]; for (int i = 0; i < fpaths.size(); i++) { if (StringUtils.isEmpty(fpaths.get(i))) { return false; } files[i] = new File(fpaths.get(i)); if (!files[i].exists() || !files[i].isFile()) { return false; } } File resultFile = new File(resultPath); try (FileOutputStream os = new FileOutputStream(resultFile, true); FileChannel resultFileChannel = os.getChannel();) { for (int i = 0; i < fpaths.size(); i++) { try (FileChannel blk = new FileInputStream(files[i]).getChannel();) { resultFileChannel.transferFrom(blk, resultFileChannel.size(), blk.size()); } } } catch (Exception e) { log.error("merge file error", e); return false; } for (int i = 0; i < fpaths.size(); i++) { files[i].delete(); } return true; } }