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

前端 未结 9 1930
夕颜
夕颜 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:57

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

提交回复
热议问题