37

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
drye
  • 1,362
  • 2
  • 15
  • 25

6 Answers6

31

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

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.

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

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765
26

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.

John Topley
  • 113,588
  • 46
  • 195
  • 237
  • 1
    This works like a charm! You can also move to Mac OS X Trash. – Paul Lammertsma Oct 01 '09 at 10:56
  • 1
    It doesn't work with iso-8859-1 encoding, if your program is used on a computer using special characters (éèàñ...) in the path it won't work... but it's fine on an english computer – Setsuki Dec 24 '11 at 16:31
  • @PaulLammertsma Which one works for Mac OS X, John's answer or drye's? – Igor Feb 22 '13 at 09:59
  • IIRC, the second link ("Send To Recycle Bin") works on both Windows and OS X because it simply executes the default action for deleting a file with flag `FOF_ALLOWUNDO`. – Paul Lammertsma Feb 22 '13 at 11:52
  • 1
    Anything that uses `SHFileOperation` can't work on OS X because `SHFileOperation` is a Windows' API. – John Topley Feb 22 '13 at 12:30
  • @JohnTopley So it doesn't work on _Mac OS X_ ? if so, do you know any other way that works on Macs too? – Igor Feb 25 '13 at 23:01
4

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

Ahmad Sayeed
  • 344
  • 2
  • 14
2

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 2
    Neither link works. [here](http://stackoverflow.com/questions/3709492/java-on-windows-how-to-delete-a-file-to-trash-using-jna) is an example – TAAPSogeking Jan 04 '14 at 23:45
0

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

}
DamianoPantani
  • 1,168
  • 2
  • 13
  • 23
0

In JNA platform, the FileUtils doesn't use Win32 API. You should prefer W32FileUtils which supports Undo (restore the file from recycle bin).

Edit: as of the current version of JNA Platform (5.7.0), with FileUtils.getInstance(), this statement has become incorrect, and FileUtils will use the Win32 API.

Antonio Noack
  • 309
  • 3
  • 11
Christophe Moine
  • 816
  • 1
  • 7
  • 7