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

前端 未结 9 1822
夕颜
夕颜 2020-12-13 18:12

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

9条回答
  •  囚心锁ツ
    2020-12-13 18:51

    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

提交回复
热议问题