How to delete a folder with files using Java

后端 未结 28 3571
北荒
北荒 2020-11-28 05:36

I want to create and delete a directory using Java, but it isn\'t working.

File index = new File(\"/home/Work/Indexer1\");
if (!index.exists()) {
    index.m         


        
28条回答
  •  忘掉有多难
    2020-11-28 05:55

    You can make recursive call if sub directories exists

    import java.io.File;
    
    class DeleteDir {
    public static void main(String args[]) {
    deleteDirectory(new File(args[0]));
    }
    
    static public boolean deleteDirectory(File path) {
    if( path.exists() ) {
      File[] files = path.listFiles();
      for(int i=0; i

提交回复
热议问题