目前我用以下代码删除文件,但它不起作用。我以为可能是权限问题或其他原因,但事实并非如此。我正在测试的文件为空且存在,所以不确定为什么它不能被删除。
UserInput.prompt("Enter name of file to delete");
String name = UserInput.readString();
File file = new File("\\Files\\" + name + ".txt");
file.delete();
任何帮助都将不胜感激!我现在有:
File file = new File(catName + ".txt");
String path = file.getCanonicalPath();
File filePath = new File(path);
filePath.delete();
为了在运行时尝试找到正确的路径,以便如果程序被转移到另一台计算机上,它仍然能够找到文件。
file.delete()
调用的返回值是什么? - Brad Mace