Share this page 

Move files to Windows Trash (JNA)Tag(s): JNA





// JNA JARS

// https://github.com/twall/jna#readme

// you need 2 jars : jna-3.5.1.jar and platform-3.5.1.jar



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

        }

    }

}

  • Works for Windows and Mac
  • On Windows, a file on a network share will be deleted but not moved to the Trash.