无法使用线程删除文件

发布于 2024-10-21 01:09:32 字数 1136 浏览 4 评论 0原文

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

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

    }

}

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

I used the following codes but i am unable to delete the file. Can anyone help?

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

    }

}

Or is there any other way to delete a file using threads?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青衫儰鉨ミ守葔 2024-10-28 01:09:32

这就是您正在寻找的。

public class Delete extends Thread {

    public static void main(String[] args) {

        Thread a = new Delete();

        a.start();

    }

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

This is what you are looking for.

public class Delete extends Thread {

    public static void main(String[] args) {

        Thread a = new Delete();

        a.start();

    }

    public void run() {
        // your implementation
    }
}
起风了 2024-10-28 01:09:32

run() 方法没有被调用

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

    }

}

The method run() is not called

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

    }

}
等你爱我 2024-10-28 01:09:32

您必须使用以下内容启动线程:

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

更新:

您的Delete类还需要实现Runnable

You have to start your thread with:

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

Update:

Your Delete class also needs to implement Runnable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文