godles

删除多级文件夹

public class MyTest2 {
    public static void main(String[] args) {
        //删除多级目录
        File file = new File("E:\\demo");
        deleteFolder(file);
    }

    private static void deleteFolder(File file) {
        //获取此目录下所有的文件或者目录
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isFile()) {
                f.delete();
            } else {
                deleteFolder(f);
            }
        }
        file.delete();//删除自身这个空文件夹
    }
}

判断单极文件夹下是否有.jpg 结尾的文件,如果有,把后缀名改成 .png

public class MyTest {
    public static void main(String[] args) {
        //判断一个目录下是否有.jpg 结尾的文件,如果有,把后缀名改成 .png
        File file = new File("E:/demo");
        //获取此目录下所有的文件 或目录
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isFile() && f.getName().endsWith(".jpg")) {
                String absolutePath = f.getAbsolutePath();
                String substring = absolutePath.substring(0, absolutePath.lastIndexOf("."));
                File newFile = new File(substring + ".png");
                f.renameTo(newFile);
            }else{
                System.out.println("不存在需要的文件");
                
            }
        }
    }
}

分类:

技术点:

相关文章:

  • 2021-11-06
  • 2021-11-05
  • 2021-08-06
  • 2021-09-17
  • 2021-12-12
  • 2021-11-17
  • 2022-02-22
  • 2021-11-23
猜你喜欢
  • 2021-04-21
  • 2021-12-25
  • 2021-10-03
  • 2021-10-17
  • 2022-01-07
  • 2022-02-05
相关资源
相似解决方案