【Java文件操作】1.找出2个目录中的同名文件 2.父文件夹不存在则创建

 1.

import java.io.File;
import java.util.HashMap;

public class Main {
    public static void listDir(File file, HashMap<String, String> namePathMap) {
        if (file.isDirectory()) {
            // 是目录则递归调用
            File[] results = file.listFiles();
            for (int i = 0; i < results.length; i++) {
                listDir(results[i], namePathMap);
            }
        } else {
            if (file != null && file.isFile()) {
                String name = file.getName();
                if (name.endsWith(".js") || name.endsWith(".ts")) {
                    String path = file.getAbsolutePath();
                    namePathMap.put(name, path);
                }
            }
        }
    }

    public static void main(String[] args) {
        // 得到Framework下面的文件
        HashMap<String, String> frameworkNamePathMap = new HashMap<>();
        File file = new File("/Users/jianan/Documents/old_work/sayabc-gamification-demo01-all/MathClassroomH5/Project/IFTTTSample/assets/Framework");
        listDir(file, frameworkNamePathMap);

        // mario上的文件
        HashMap<String, String> marioNamePathMap = new HashMap<>();
        File marioFile = new File("/Users/jianan/Documents/new_work/mario/courseware/assets");
        listDir(marioFile, marioNamePathMap);

        // TODO 第1步: 保证Framework下面不多删
        // Framework中的脚本,mario上都可以找到. 这样保证不会多删除。
        System.err.println("下面的文件不可以放在Framework目录下");
        for (String key : frameworkNamePathMap.keySet()) {
            if (marioNamePathMap.get(key) == null) {
                System.out.println(key + ":" + frameworkNamePathMap.get(key));
            }
        }

        // TODO 第2步: 不少删
        // mario上有的,旧工程也有,那么必然是在Framework中。
        HashMap<String, String> oldNamePathMap = new HashMap<>();
        File oldFile = new File("/Users/jianan/Documents/old_work/sayabc-gamification-demo01-all/MathClassroomH5/Project/IFTTTSample/assets");
        listDir(oldFile, oldNamePathMap);

        System.err.println("应该放到Framework目录下的文件");
        for (String key : marioNamePathMap.keySet()) {
            if (oldNamePathMap.get(key) != null) {
                String path = oldNamePathMap.get(key);

                int idx = path.indexOf("MathClassroomH5/Project/IFTTTSample/assets/Framework/");

                if (idx == -1) {
                    System.out.println(key + ":" + path);
                }
            }
        }

    }
}

/*
下面的文件不可以放在Framework目录下
应该放到Framework目录下的文件
 */

2.

package org.example.testFile2;

import java.io.File;

public class Main {
	public static void main(String[] args) throws Exception {
		String path = "D:/csv/csv";

		String fileName = "test";

		String filePath = path + "/" + fileName + ".csv";

		File file = new File(filePath);

		// 父文件夹不存在则创建
		File parentFile = file.getParentFile();
		if (parentFile != null) {
			parentFile.mkdirs();
		}

		if (!file.exists()) {
			file.createNewFile();
		}

	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值