Is it possible with Java to delete to the Recycle

2019-01-22 18:25发布

Java is the key here. I need to be able to delete files but users expect to be able to "undelete" from the recycle bin. As far as I can tell this isn't possible. Anyone know otherwise?

8条回答
Luminary・发光体
2楼-- · 2019-01-22 18:38

Java 9 has new method but in my case I am restricted to Java 8. I found Java Native Access Platform that has hasTrash() and moveToTrash() method. I tested it on Win 10 and Mac OS (Worked) for me.

static boolean moveToTrash(String filePath) {
        File file = new File(filePath);

        FileUtils fileUtils =  FileUtils.getInstance();
        if (fileUtils.hasTrash()) {

            try {
                fileUtils.moveToTrash(new File[] { file });
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        } else {
            System.out.println("No Trash");
            return false;
        }
    }

Maven Repository https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform/5.1.0

Don't confuse It is Java Native Access Platform not Java Native Access

查看更多
仙女界的扛把子
3楼-- · 2019-01-22 18:42

I have found this RFE on suns site: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5080625

This tells me there is not a native java way to do this. and as @John Topley just posted the only solution is a JNI call.

查看更多
等我变得足够好
4楼-- · 2019-01-22 18:44

My 3 cents - use cmd util Recycle.exe with -f to force recycle (no prompt). Works perfectly.

public class Trash {

    public void moveToTrash(File ... file) throws IOException {
        moveToTrash(false, file);
    }

    public void promptMoveToTrash(File ... file) throws IOException {
        moveToTrash(true, file);
    }

    private void moveToTrash(boolean withPrompt, File ... file) throws IOException {
        String fileList = Stream.of(file).map(File::getAbsolutePath).reduce((f1, f2)->f1+" "+f2).orElse("");
        Runtime.getRuntime().exec("Recycle.exe "+(withPrompt ? "" : "-f ")+fileList);
    }

}
查看更多
贪生不怕死
5楼-- · 2019-01-22 18:47

See the fileutil incubator project (part of the Java Desktop Integration Components project):

This incubator project is created to host those file utility functionalities, most of which are extensions to the java.io.File class in J2SE. There are frequent requests from Java developers for such features like: sending a file to trash bin, checking free disk space, accessing file attributes etc. This project addresses such frequently requested APIs.

Note, this should work not only on Windows, but on other platforms (Linux, Mac OS X) as well.

查看更多
别忘想泡老子
6楼-- · 2019-01-22 18:53

For various reasons Windows has no concept of a folder that simply corresponds to the Recycle Bin.

The correct way is to use JNI to invoke the Windows SHFileOperation API, setting the FO_DELETE flag in the SHFILEOPSTRUCT structure.

查看更多
Luminary・发光体
7楼-- · 2019-01-22 18:53

As John Topley suggests, you must do this with a native operation. In case you don't want to get your hands dirty with some JNI, you could use a library called Java Native Access to do the native calls.

查看更多
登录 后发表回答