Java 實例 - 刪除檔
以下實例演示了使用 delete() 方法將檔刪除:
Main.java 檔
import java.io.*;
public class Main
{
public static void main(String[] args)
{
try{
File file = new File("c:\\test.txt");
if(file.delete()){
System.out.println(file.getName() + " 檔已被刪除!");
}else{
System.out.println("檔刪除失敗!");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
以上代碼運行輸出結果為(需要在 C 盤上先創建 test.txt 檔):
test.txt 檔已被刪除!