package com.tij.io.file;
import java.io.File;
public class DeleteFileJava {
/**
* This is Shows How to delete a File in Java
* @param args
*/
public static void main(String[] args) {
//absolute file name with path
File file = new File("/Users/GYJ/file.txt");
if (file.delete()) {
System.out.println("/Users/GYJ/file.txt" + "File deleted");
} else {
System.out.println("File /Users/GYJ/file.txt doesn't exists");
}
//file name only
file = new File("file.txt");
if (file.delete()) {
System.out.println("file.txt File deleted from Project root directories");
} else {
System.out.println("File file.txt doesn't exists in Project root directories");
}
//relative path
file = new File("temp/file.txt");
if (file.delete()) {
System.out.println("tmp/file.txt File deleted from Project root directory");
} else {
System.out.println("File tmp/file.txt doesn't exists in project root directory");
}
//delete empty directory
file = new File("temp");
if (file.delete()) {
System.out.println("temp directory deleted from Project root directory");
} else {
System.out.println("temp dierctory doesn't or not empty in Project root directory");
}
//try to delete directory with files
file = new File("/Users/GYJ/project");
if (file.delete()) {
System.out.println("/Users/GYJ/project directory deleted from Project root directory");
} else {
System.out.println("/Users/GYJ/project directory doesn't exists or not empty");
}
}
}
out put=================
File /Users/GYJ/file.txt doesn't exists
File file.txt doesn't exists in Project root directories
tmp/file.txt File deleted from Project root directory
temp directory deleted from Project root directory
/Users/GYJ/project directory doesn't exists or not empty
2、java如何删除文件
最新推荐文章于 2024-08-24 17:09:16 发布