So I'm trying to create a program in Unix that will take in a directory as a parameter and then recursively go through, open all of the folders, look through all of the files, and then delete all of the class files. I thought I was taking the correct steps as I was given code for a similar program and told to use it as a basis, but upon testing my program and I discover that nothing happens.
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
public class ClassFileDeleter {
public static void main(String[] args) throws ParseException {
String dirName = args[0];
deleteFile(dirName);
}
private static void deleteFile(String dirName) {
Path path = Paths.get(dirName);
File dir = path.toFile();
if(dir.exists()) {
File[] files = dir.listFiles();
if(dir.isDirectory()) {
for(File f:files) {
if(!f.isDirectory())
if(f.toString().endsWith(".class"))
System.out.println("yes");
else deleteFile(dirName + "/" + f.getName());
}}}
}}
I am at a loss at what I should do. I haven't attempted to delete anything yet because I don't want to delete anything that isn't a class file so I am using some dummy code that should print 'yes' once the program finds a class file. However when I run my code, absolutely nothing happens. I believe that there is either an issue with the way I am searching for class files (We are supposed to use endsWith) or with the way I am attempting to use recursion to look through all of the files in the specified directory. If I could have some assistance, that would be great.