[英]File won't remove
I am trying to remove a file in java, but it will not remove. 我正在尝试删除Java中的文件,但不会删除。 Could someone explain why it won't remove? 有人可以解释为什么它不能删除吗?
Here is the code that I am using: 这是我正在使用的代码:
File bellFile = new File("config\\normbells.txt");
bellFile.delete();
File bellFileNew = new File("config\\normbells.txt");
bellFileNew.createNewFile();
System.out.println("Done!");
NOTE: I am trying to wipe the file, if that helps. 注意:如果有帮助,我正在尝试擦除文件。
File deletion can fail under the following circumstances: 在以下情况下文件删除可能会失败:
Try avoiding all the above mentioned circumstances & you'll surely able to delete the file. 尽量避免上述所有情况,并且您一定可以删除该文件。 Also before deleting the file add this condition : 同样在删除文件之前,请添加以下条件:
if (file.exists()) {
file.delete();
}
Java7 has new functionality for this. Java7为此提供了新功能。
Path target = Paths.get("D:\\Backup\\MyStuff.txt");
Files.delete(target);
Path newtarget = Paths.get("D:\\Backup\\MyStuff.txt");
Set<PosixFilePermission> perms
= PosixFilePermissions.fromString("rw-rw-rw-");
FileAttribute<Set<PosixFilePermission>> attr
= PosixFilePermissions.asFileAttribute(perms);
Files.createFile(newtarget, attr);
Take a look at the File class http://docs.oracle.com/javase/7/docs/api/java/io/File.html 看一下File类http://docs.oracle.com/javase/7/docs/api/java/io/File.html
File bellFile = new File("config\\normbells.txt");
if(bellFile.delete())
{
System.out.println("Done!");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.