简体   繁体   English

Java - 如何从 Java 中的文件夹中删除文件?

[英]Java - How to delete files from a folder in Java?

I'm trying to delete files using a for-loop.我正在尝试使用 for 循环删除文件。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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