Java on Windows: how to delete a file to trash (us

2019-02-13 20:56发布

问题:

I'm not experiences with Windows API at all, so please excuse my ignorance.

I want to delete files to the trash. How to do that using JNA and how to detect if this would not be possible, e.g., because the files are located on a network share?

回答1:

Use com.sun.jna.platform.win32.W32FileUtils, which has moveToTrash and hasTrash methods defined.



回答2:

Use com.sun.jna.platform.FileUtils instead of com.sun.jna.platform.win32.W32FileUtils directly.

import java.io.File;
import java.io.IOException;

import com.sun.jna.platform.FileUtils;

public class MoveToTrash {

    public static void main(String[] args){
        FileUtils fileUtils = FileUtils.getInstance();
        if (fileUtils.hasTrash()) {
            try {
                fileUtils.moveToTrash( new File[] {new File("c:/temp/dummy.txt") });                
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        else {
            System.out.println("No Trash available");
        }
    }
}


标签: java windows jna