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

↘锁芯ラ 提交于 2019-12-30 06:53:14

问题


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


来源:https://stackoverflow.com/questions/3709492/java-on-windows-how-to-delete-a-file-to-trash-using-jna

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!