Delete files with extension current directory Java

Delete files with extension current directory Java

If you want to delete files from current folder with certain extension please use below sample java program:

  • Sample java class:
package com.javahonk;

import java.io.File;

public class DeleteFilesCurrentDirectory {

	public static void main(String[] args) {
		
		File folder = new File("C:\\JavaHonk\\zip\\SpringMVCRESTFulService");
		File listOfFiles[] = folder.listFiles();
		
		for (File file : listOfFiles) {
			
			String fileName  = file.getName();
			
			if (null != file && fileName.endsWith(".java")) {
				Boolean success = (file.delete());
				
				if (success) {
					System.out.println(file.getAbsolutePath()+" delted successfully");
				}
			}
		}
		
	}

}
  • Output:

Delete files with extension current directory Java

For more information delete files please refer oracle documentation here

Leave a Reply

Your email address will not be published. Required fields are marked *