简体   繁体   中英

Java - How to delete files from a folder in Java?

I'm trying to delete files using a for-loop.

for(int i = 0; i < 3; i++) {

   String pathName = "test" + i + ".csv";
   boolean success = (new File(pathName)).delete();

}

But this code deletes only the first file, but not the other ones.

This worked for me. Maybe the path where your files are located is incorrect.

for(int i = 1; i <= 3; i++) {
    boolean success = (new File("C:\\Users\\Suppada-Aide\\Desktop\\test"+i+".txt")).delete();
    System.out.println(success);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM