Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java on Windows: how to delete a file to trash (using JNA)

Tags:

java

windows

jna

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?

like image 876
Mot Avatar asked Sep 14 '10 13:09

Mot


2 Answers

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");
        }
    }
}
like image 59
RealHowTo Avatar answered Oct 04 '22 01:10

RealHowTo


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

like image 42
dB. Avatar answered Oct 04 '22 00:10

dB.