我使用以下代码删除文本文件。但它不会删除仍然存在的文件。
File delfile=new File("SDS_DID.txt");
delfile.delete();
如何删除文件?
答案 0 :(得分:2)
答案 1 :(得分:1)
最常见的问题是找不到文件。 确保该文件已存在。
String fileName = "SDS_DID.txt";
// A File object to represent the filename
File f = new File(fileName);
if (!f.exists) {
JOptionPane.showErrorDialog (null , filename + " is not found.");
}
你不能只在硬盘上的任何地方拿起一个文件,你必须告诉它实际上在哪里。
答案 2 :(得分:0)
System.gc(); // close any streams first
String fileName = "SDS_DID.txt";
// A File object to represent the filename
File f = new File(fileName);
// Make sure the file or directory exists and isn't write protected
if (!f.exists())
throw new IllegalArgumentException(
"Delete: no such file or directory: " + fileName);
if (!f.canWrite())
throw new IllegalArgumentException("Delete: write protected: "
+ fileName);
// Attempt to delete it
boolean success = f.delete();
if (!success)
throw new IllegalArgumentException("Delete: deletion failed");