Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用线程删除文件_Java_Multithreading_File - Fatal编程技术网

Java 无法使用线程删除文件

Java 无法使用线程删除文件,java,multithreading,file,Java,Multithreading,File,我使用了以下代码,但无法删除该文件。有人能帮忙吗 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"; /

我使用了以下代码,但无法删除该文件。有人能帮忙吗

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();

    }

}

或者是否有其他方法可以使用线程删除文件?

您必须从以下内容开始执行线程:

Thread a = new Thread(new Delete());
a.start();
更新:


您的Delete类还需要实现Runnable。

这就是您想要的

public class Delete extends Thread {

    public static void main(String[] args) {

        Thread a = new Delete();

        a.start();

    }

    public void run() {
        // your implementation
    }
}

未调用运行的方法

import java.io.File;

public class Delete extends Thread {

    public static void main(String[] args) {

        Delete a = new Delete();
        a.start();
    }

    public void run()
    {
        String fileName = "C:\\temp\\todelete.txt";
        File f = new File(fileName);
        if (!f.exists())
            throw new IllegalArgumentException("Delete: no such file or directory: " + fileName);
        if (!f.canWrite())
            throw new IllegalArgumentException("Delete: write protected: " + fileName);
        if (f.isDirectory()) {
            String[] files = f.list();
            if (files.length > 0)
                throw new IllegalArgumentException("Delete: directory not empty: " + fileName);
        }

        boolean success = f.delete();

        if (!success)
            throw new IllegalArgumentException("Delete: deletion failed");

    }

}

缩进,阅读线程基础知识,然后回来。你可以删除。交叉张贴在这里:为什么你要用格式重新张贴问题中的代码?这有什么帮助?还是我遗漏了什么?嗯,你是对的。除了延长线部分,没有什么不同@迈克尔-不是我!原作者在他的问题中补充了这一点:啊,错过了。但它确实已经运行了。