This is how to delete the files in the directory and the directories with or without files. 

import java.io.File;

public class FileTest {

	public static void main(String[] args) {

		try {
			File temp = new File("C:/java01", "temp");
			File tempFile = new File("test"); // Relative path
			temp.mkdirs();
			tempFile.mkdirs();

			File[] f = tempFile.listFiles();
			for (int i = 0; i < f.length; i++) {
				System.out.println(f[i].getName());
				f[i].delete(); // To delete all files in the directory
			}
			tempFile.delete(); // To delete the empty directory

//To delete a child directory
			temp.delete(); 

//To delete a parent directory
			temp.getParentFile().delete();

		} catch (Exception e) {
		}
	}
}

'Java' 카테고리의 다른 글

Java) Array  (0) 2022.09.04
Java) .length vs length()  (0) 2022.09.02
Java) Conditional statements and loops  (0) 2022.08.30
Java) Data Types  (0) 2022.08.27
Java) Operators  (0) 2022.08.26

+ Recent posts