Delete all files with an extension using Java

后端 未结 5 1361
自闭症患者
自闭症患者 2021-02-15 11:52

I\'m (relatively) new to Java and I\'m trying to implement a .jar that runs a list of commands that in Windows XP\'s command prompt it would be:

cd\\
cd myfolder         


        
5条回答
  •  温柔的废话
    2021-02-15 12:45

    fList.get(i) should be fList[i] as fList is an array, and it returns a File reference not a String.

    Change: -

    String pes = fList.get(i);
    

    to: -

    File pes = fList[i];
    

    And then change if (pes.contains(".lck") == true) to
    if (pes.getName().contains(".lck"))

    In fact, since you are checking for the extension, you should use endsWith method rather than contains method. And yes, you don't need to compare your boolean value with ==. So just use this condition: -

    if (pes.getName().endsWith(".lck")) {
        boolean success = (new File(fList.get(i)).delete());
    }
    

提交回复
热议问题