java 刪除指定路徑下的檔案
阿新 • • 發佈:2018-12-31
import java.io.File;
/**
* @param args
*/
public static void main(String[] args){
System.out.println("輸入檔案路徑:");
Scanner sc= new Scanner(System.in);
String path = sc.next();
deleteFiles(path);
}
public static void deleteFiles(String path){
File file = new File(path);
//1級檔案刪除
if(!file.isDirectory()){
file.delete();
}else if(file.isDirectory()){
//2級檔案列表
String []filelist = file.list();
//獲取新的二級路徑
for(int j=0;j<filelist.length;j++){
File filessFile= new File(path+"\\"+filelist[j]);
if(!filessFile.isDirectory()){
filessFile.delete();
}else if(filessFile.isDirectory()){
//遞迴呼叫
deleteFiles(path+"\\"+filelist[j]);
}
}
file.delete();
}
}
}
import java.util.Scanner;
public class deleteFile{/**
* @param args
*/
public static void main(String[] args){
System.out.println("輸入檔案路徑:");
Scanner sc= new Scanner(System.in);
String path = sc.next();
deleteFiles(path);
}
public static void deleteFiles(String path){
File file = new File(path);
//1級檔案刪除
if(!file.isDirectory()){
file.delete();
}else if(file.isDirectory()){
//2級檔案列表
String []filelist = file.list();
//獲取新的二級路徑
for(int j=0;j<filelist.length;j++){
File filessFile= new File(path+"\\"+filelist[j]);
if(!filessFile.isDirectory()){
filessFile.delete();
}else if(filessFile.isDirectory()){
//遞迴呼叫
deleteFiles(path+"\\"+filelist[j]);
}
}
file.delete();
}
}
}