无法使用线程删除文件 [英] Can't delete a file using threads

查看:87
本文介绍了无法使用线程删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

这就是您要寻找的.

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
    }
}

这篇关于无法使用线程删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆