java Java可以删除到回收站吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/222463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:30:00  来源:igfitidea点击:

Is it possible with Java to delete to the Recycle Bin?

javarecycle-bin

提问by drye

Javais 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?

Java是这里的关键。我需要能够删除文件,但用户希望能够从回收站“取消删除”。据我所知,这是不可能的。有谁知道别的吗?

采纳答案by John Topley

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

出于各种原因,Windows没有与回收站简单对应的文件夹的概念

The correct way is to use JNI to invoke the Windows SHFileOperationAPI, setting the FO_DELETEflag in the SHFILEOPSTRUCTstructure.

正确的方法是使用 JNI 调用 Windows SHFileOperationAPI,FO_DELETESHFILEOPSTRUCT结构中设置标志。

回答by Holger

Ten years later, with Java?9, finally there is a builtin way to move files to the Trash Bin

十年后,使用 Java?9,终于有一个内置的方法可以将文件移动到垃圾箱

java.awt.Desktop.moveToTrash(java.io.File)java.awt.Desktop.moveToTrash(java.io.File)

public boolean moveToTrash?(File file)

Moves the specified file to the trash.

Parameters:

file - the file

Returns:

returns true if successfully moved the file to the trash.

public boolean moveToTrash?(File file)

将指定的文件移动到垃圾箱。

参数:

文件 - 文件

返回:

如果成功将文件移动到垃圾箱,则返回 true。

The availability of this feature for the underlying platform can be tested with Desktop.isSupported?(Desktop.Action.MOVE_TO_TRASH).

可以使用 测试此功能对底层平台的可用性Desktop.isSupported?(Desktop.Action.MOVE_TO_TRASH)

回答by Ahmad Sayeed

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

Java 9 有新方法,但就我而言,我仅限于 Java 8。我发现Java Native Access Platform具有hasTrash()moveToTrash()方法。我在 Win 10 和 Mac OS (Worked) 上为我测试了它。

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

Maven 存储库 https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform/5.1.0

Don't confuse It is Java Native Access Platformnot Java Native Access

不要混淆它是Java Native Access Platform而不是 Java Native Access

回答by Jesper

See the fileutilincubator project (part of the Java Desktop Integration Componentsproject):

查看fileutil孵化器项目(Java 桌面集成组件项目的一部分):

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.

创建这个孵化器项目是为了承载那些文件实用程序功能,其中大部分是 J2SE 中 java.io.File 类的扩展。Java 开发人员经常要求此类功能,例如:将文件发送到垃圾箱、检查可用磁盘空间、访问文件属性等。该项目解决了此类频繁请求的 API。

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

请注意,这不仅适用于 Windows,还适用于其他平台(Linux、Mac OS X)。

回答by drye

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

我在太阳网站上找到了这个 RFE: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.

这告诉我没有本地 Java 方法可以做到这一点。正如@John Topley 刚刚发布的那样,唯一的解决方案是 JNI 调用。

回答by Guemundur Bjarni

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 Accessto do the native calls.

正如 John Topley 所建议的,您必须使用本机操作来执行此操作。如果您不想接触某些 JNI,您可以使用名为Java Native Access的库来执行本机调用。

回答by Damiano

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

我的 3 美分 - 使用 cmd util Recycle.exe和 -f 强制回收(无提示)。完美运行。

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

}

回答by Christophe Moine

In JNA platform, the FileUtilsdoesn't use Win32 API. You should prefer W32FileUtilswhich supports Undo (restore the file from recycle bin).

在 JNA 平台中,FileUtils不使用 Win32 API。您应该更喜欢W32FileUtils哪个支持撤消(从回收站中恢复文件)。