java 文件夹合并

      前一段时间android项目中用到文件夹合并,百度一下尽然没有合适的,自己写了一个递归处理的,还可以简化,但是稳定运行了,也就没有继续修改了。


public classFileTools {

private String mSourcePath;
private String mDesPath;
private boolean mResult;

private void FileTools() {
}

static class Instance {
    private static FileTools fileTools = new FileTools();
}

public static FileTools getInstance() {
    return Instance.fileTools;

}
/**
 * 用于同级合并两个文件夹
 *
 * @param sourceFolderPath 源文件的绝对路径  /data/com.dazhihui/temp
 * @param desFolderPath    目的文件的 /data/com.dazhihui/web
 * @return
 */
public boolean mergeFolder(String sourceFolderPath, String desFolderPath) {
    if (TextUtils.isEmpty(sourceFolderPath)) {
        return true;
    }
    if (TextUtils.isEmpty(desFolderPath)) {
        return false;
    }
    this.mSourcePath = sourceFolderPath;
    this.mDesPath = desFolderPath;
    File sourceFile = new File(mSourcePath);
    mResult = true;
    try {
        File[] fileList = sourceFile.listFiles();
        for (File file :
                fileList) {
            merge(file);
        }
    } catch (Exception e) {
        return false;
    }
    return mResult;

}

private void merge(File sourceFile) {
    //先找源文件相当于初始路劲的相对路径 才能找到目的文件的路径
    String sourcePath = sourceFile.getAbsolutePath();
    String relativePath = sourcePath.substring(mSourcePath.length() + 1);
    String desPath = mDesPath + File.separator + relativePath;
    File findDesFile = new File(desPath);
    if (sourceFile.isDirectory()) {
        //目录合并  即判断目的文件里面有没有该目录 有则不动 没有则创建
        if (findDesFile.exists()) {
        } else {
            findDesFile.mkdirs();
        }
        File[] fileList = sourceFile.listFiles();
        for (File file :
                fileList) {
            merge(file);
        }
    } else {
        //源文件为文件 则替换
        if (findDesFile.exists()) {
            findDesFile.delete();
        }
        sourceFile.renameTo(findDesFile);
    }
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!很高兴为您解答问题。针对您的问题,可以使用Apache POI的XSSF API来实现。具体步骤如下: 1. 定义待合并的Excel文件夹路径和合并后的Excel文件名; 2. 使用File类遍历文件夹下的所有Excel文件,获取文件路径; 3. 使用XSSFWorkbook类打开每个Excel文件,并使用getSheetAt()获取指定Sheet; 4. 获取指定区域的行和列,通过for循环遍历每一行和每一列,将数据写入已创建的合并Excel文件中; 5. 关闭所有打开的Excel文件。 示例代码如下: ``` import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelMerger { public static void main(String[] args) throws IOException { // 定义待合并的Excel文件夹路径和合并后的Excel文件名 String folderPath = "C:\\excel_folder\\"; String mergedFileName = "merged_excel.xlsx"; String mergedFilePath = folderPath + mergedFileName; // 创建合并后的Excel文件 XSSFWorkbook mergedWorkbook = new XSSFWorkbook(); XSSFSheet mergedSheet = mergedWorkbook.createSheet("merged sheet"); // 遍历文件夹,获取Excel文件路径 File folder = new File(folderPath); File[] files = folder.listFiles(); for (File file : files) { String filePath = file.getAbsolutePath(); if (!filePath.endsWith(".xlsx")) { continue; } // 打开Excel文件 XSSFWorkbook workbook = new XSSFWorkbook(filePath); Sheet sheet = workbook.getSheetAt(0); // 获取指定区域的行和列,写入合并Excel文件 int startRow = 1; int endRow = 10; int startCol = 1; int endCol = 5; for (int i = startRow; i <= endRow; i++) { XSSFRow mergedRow = mergedSheet.createRow(i); for (int j = startCol; j <= endCol; j++) { XSSFCell mergedCell = mergedRow.createCell(j); XSSFCell cell = (XSSFCell) sheet.getRow(i).getCell(j); mergedCell.setCellValue(cell.toString()); } } // 关闭打开的Excel文件 workbook.close(); } // 输出合并后的Excel文件 OutputStream os = new FileOutputStream(mergedFilePath); mergedWorkbook.write(os); mergedWorkbook.close(); os.close(); } } ``` 希望能解决您的问题,如有不明白的地方或其他问题,欢迎继续咨询!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值