search
HomeJavajavaTutorialUse JAVA code to delete all empty folders on the disk

这篇文章主要介绍了JAVA实现将磁盘中所有空文件夹进行删除的代码,需要的朋友可以参考下

实现代码一、

import java.io.File;
import java.io.*;
public class DelNullDir {
	public void ShowDir(File f){
		for (File f1:f.listFiles()){
			if(f1.isDirectory()){
				ShowDir(f1);
				//一直递归到最后的目录
				if(f1.listFiles().length==0){
					//如果是文件夹里面没有文件证明是空文件,进行删除
					f1.delete();
				}
			}
		}
	}
	/**
 * 
 * 把磁盘中所有空的文件夹进行删除
 */
	public static void main(String[] args) {
		File f = new File("F:\\360CloudUI\\");
		new DelNullDir().ShowDir(f);
	}
}

实现代码二、

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
/**
 * Description:递归方式,扫描并删除磁盘中的空文件夹
 *(C盘中的一些空文件夹是系统文件夹,删除多次后空文件夹数量不变,则停止此程序)
 * */
public class DelEmptyFolders {
  //空文件夹的绝对路径
  private static StringBuffer paths;
  //本次扫描的空文件夹的数量
  private static int cnt;
  public static void main(String[] args) {
    boolean flag = true;
    do{
      cnt = 0;
      paths = new StringBuffer();
      long start = new Date().getTime();
      System.out.println("正在扫描......");
      //要扫描的磁盘
      File disk = new File("C:/");
      //日志文件的位置
      File log = new File("D:/scanLog_C.txt");
      try {
        //扫描磁盘
        scanEmptyFolders(disk);
        //空文件夹数大于0时,将文件夹的绝对路径记录到日志中并再扫描一次;否则停止扫描
        if(cnt > 0){
          fileWrite(paths.toString(), log);
        }else{
          flag = false;
        }
      } catch (FileNotFoundException e1) {
        e1.printStackTrace();
      } catch (IOException e2) {
        e2.printStackTrace();
      }
      long end = new Date().getTime();
      System.out.println("本次扫描完毕,耗时:"+(end-start)/1000+" 秒,共删除:"+cnt+" 个空文件夹!\n");
    }while(flag);
  }
  /**
   * TODO:递归扫描空文件夹
   * @throws UnsupportedEncodingException
   * */
  private static void scanEmptyFolders(File file) throws UnsupportedEncodingException{
    if(file != null && file.isDirectory()){
      File[] files = file.listFiles();
      //非空文件夹
      if(files != null){
        if(files.length > 0){
          for (File temp : files) {
            scanEmptyFolders(temp);
          }
        }else{
          System.out.println(file.getAbsolutePath());
          //记录日志
          paths.append(new String((file.getAbsolutePath()+"\r\n").getBytes(),"UTF-8"));
          cnt++;
          //删除空文件夹
          file.delete();
        }
      }
    }
  }
  /**
   * TODO:将字符串写入文本文件
   * @throws IOException
   * */
  private static void fileWrite(String info,File file) throws IOException{
    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bos.write(info.getBytes());
    bos.flush();
    bos.close();
    fos.close();
  }
}

The above is the detailed content of Use JAVA code to delete all empty folders on the disk. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.