java刪除文件的方法:
/* 使用File的delete()方法刪除文件
*/
import java.io.*;
public class Exercise {
public static void main(String args[]) {
try {
// Specify the file name and path
File file = new File("/home/zjz/Desktop/myFile.txt");
/* the delete() method return true if the file
deleted successfully else it return false
*/
if (file.delete()) {
System.out.println(file.getName() + "is deleted");
} else {
System.out.println("Delete failed.");
}
} catch (Exception e) {
System.out.println("Exception occured");
e.printStackTrace();
}
}
}Java文件類以抽象的方式代表文件名和目錄路徑名。該類主要用于文件和目錄的創建、文件的查找和文件的刪除等。java中刪除文件可以使用File.delete()方法實現。
Java File.delete()方法具有以下語法:
public boolean delete()
刪除此抽象路徑名表示的文件或目錄。
以上就是java怎么刪除文件的詳細內容,更多請關注億速云其它相關文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。