Java文件流刪除文件夾及文件夾中的文件

NO IMAGE
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
public class Boy{
public static void main(String[] args) throws IOException
{
String str = "E:/data";
String text = "user2.txt";
String data = "abcdefgdd";
File file = new File(str);
if(file.exists())
{
deleteAll(file);
System.out.println("刪除成功");
file.mkdir();
}
else
{
file.mkdir();
}
str = str+File.separator+text;
File f = new File(str);
OutputStream outStream = new FileOutputStream(f);
outStream.write(data.getBytes(Charset.forName("utf-8")));
outStream.flush();
outStream.close();
System.out.println("輸出成功");
}
//刪除時同時刪除指定的文件夾
public static void deleteAll(File file) {
if (file.isFile() || file.list().length == 0) {
file.delete();
//System.out.println(file.getPath());
} else {
for (File f : file.listFiles()) {
deleteAll(f); // 遞歸刪除每一個文件
}
file.delete(); // 刪除文件夾
//System.out.println(file.getPath());
}
}
}

相關文章

Springboot整合Mybatis的坑

Jboot框架的使用

Linux安裝Zookeeper

安裝React