剛剛把IO流學了一半,每次都要将eclipse中的demo截圖,轉到Word文檔中.
因為不想打十位以上的數,要就頻繁的清空截圖
看視訊裡的小練習,想到了可以試試做個清空桌面指定檔案
package cn.itcast.io.p2.filter;
import java.io.File;
/*清空指定桌面檔案夾c中的所有檔案。
*
* 1.擷取c檔案夾中的名字,用for循環,file.list,
*
* 2.将字元串數組中的值轉到字元中
*
* 2.删除指定檔案(通過檔案名)
*/
public class text {
public static void main(String[] args) {
for(String name : getName()){
NameDetele(name);
}
}
/**
* 擷取檔案夾中的所有檔案名,用name[]傳回
* @return
*/
public static String[] getName() {
File file = new File("C:\\Users\\Hp\\Desktop\\c");
String[] name = file.list();
return name;
}
/**
* 一次擷取一個檔案名,删除指定檔案
* @param name
*/
public static void NameDetele(String name) {
File file = new File("C:\\Users\\Hp\\Desktop\\c\\",name);
boolean b =file.delete();
System.out.println("b="+b);
}
}
記錄一下