Can't delete files in java?

后端 未结 5 691
广开言路
广开言路 2021-01-24 11:07

I am trying to delete all files in a folder:-

    import java.io.*;
public class AddService {   
    public static void main(String args[]){
        File folder=         


        
5条回答
  •  悲&欢浪女
    2021-01-24 11:50

    You have not given the full path for the folder. Also note to use forward slash.

    try{
            File folder=new File("C:/xxxx/xxxx/xxxx/inputs");
            File[] listOfFiles=folder.listFiles();
            for(File file:listOfFiles){
                if(file.delete())
                    System.out.println("File deleted");
                else
                    System.out.println("File not deleted");
            }
    }
    catch(Exception e)
    {
        System.out.println(e.printStackTrace());
    }
    

    Always use try-catch for code that contains methods which throws Exceptions.

提交回复
热议问题