我使用了下列代碼,但我無法刪除該文件。誰能幫忙?無法使用線程刪除文件
public class Delete{
public static void main(final String[] args){
final Thread a = new Thread();
a.start();
}
public void run(){
final String fileName = "default\\sample.txt";
// A File object to represent the filename
final 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);
}
// If it is a directory, make sure it is empty
if(f.isDirectory()){
final String[] files = f.list();
if(files.length > 0){
throw new IllegalArgumentException(
"Delete: directory not empty: " + fileName);
}
}
// Attempt to delete it
f.delete();
}
}
或者是否有任何其他方式使用線程刪除文件?
縮進,瞭解線程基礎回來。您將可以刪除。 – adarshr 2011-03-07 09:34:14
Cross-posted here:http://www.java-forums.org/threads-synchronization/40088-cant-delete-file.html – 2011-03-07 09:34:55