开发者

File handling in core java

开发者 https://www.devze.com 2023-01-23 02:28 出处:网络
I need to do some housekeeping.I accidentally setup my classpath same as my codebase and all classes ar开发者_开发百科e placed along with my code.I need to write a quick java program to select all fil

I need to do some housekeeping.I accidentally setup my classpath same as my codebase and all classes ar开发者_开发百科e placed along with my code.I need to write a quick java program to select all files of the type .class and .class alone and delete it immediately.Has anyone done something related to this?


Why don't you use the shell to do that, something like:

Linux:

 find . -name *.class -print -exec rm {} \;

Windows:

 for /r %f in (*.class) do del %f


find . -name "*.class" -exec rm '{}' \;


This might work. Untested. Those find/for commands by the others look promising, too, but just in case you are on an OS/390 mainframe here is the Java. ;-)

import java.io.File;
import java.io.IOException;

public class RemoveClass {

    public static void main(String[] args) throws Exception {
        File f = new File(".");
        deleteRecursive(f);
    }

    public static void deleteRecursive(File f) throws IOException {
        if (f.isDirectory()) {
            for (File file : f.listFiles()) {
                deleteRecursive(file);
            }
        } else if (f.isFile() && f.getName().endsWith(".class")) {

            String path = f.getCanonicalPath();

            // f.delete();
            System.out.println("Uncomment line above to delete: [" + path + "]");

        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消