1

I've used Java system clipboard to transfer text and image, but I wonder if it can copy and paste files ? If so where can I find some sample code ?

I found a similar question at : How can I copy a file and paste it to the clipboard using Java?

But nowhere in it can I find the word "clipboard", and I don't know how to use it.

The methods I use to copy image look like this :

  public static void setClipboard(Image image)                                           // This method writes a image to the system clipboard : from exampledepot.com
  {
    ImageSelection imgSel=new ImageSelection(image);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel,null);
  }

  public Image getImageFromClipboard()                                                   // Get an image off the system clipboard 
  {
    Transferable transferable=Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    if (transferable!=null&&transferable.isDataFlavorSupported(DataFlavor.imageFlavor))
    {
      try { return (Image)transferable.getTransferData(DataFlavor.imageFlavor); }
      catch (UnsupportedFlavorException e) { e.printStackTrace(); }
      catch (IOException e) { e.printStackTrace(); }
    }
    else System.err.println("getImageFromClipboard: That wasn't an image!");
    return null;
  }

How to modify the above code to transfer files ?

Community
  • 1
  • 1
Frank
  • 30,590
  • 58
  • 161
  • 244

1 Answers1

10

Essentially, yes. You need to remember that both the drag'n'drop API and the clipboard API use the same concept of a Transferable, which wraps the data into DataFlavors, so you can transfer the data differently based on the flavor the target system would like to use

Generally, when transferring files, Java uses a java.util.List and a DataFlavor.javaFileListFlavor. Unfourtantly, there's no nice "wrapper" class available for this, so you'll need to provide your own, for example...

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        File file = new File("/path/to/your/file");
        List listOfFiles = new ArrayList();
        listOfFiles.add(file);

        FileTransferable ft = new FileTransferable(listOfFiles);

        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ft, new ClipboardOwner() {
            @Override
            public void lostOwnership(Clipboard clipboard, Transferable contents) {
                System.out.println("Lost ownership");
            }
        });
    }

    public static class FileTransferable implements Transferable {

        private List listOfFiles;

        public FileTransferable(List listOfFiles) {
            this.listOfFiles = listOfFiles;
        }

        @Override
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[]{DataFlavor.javaFileListFlavor};
        }

        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return DataFlavor.javaFileListFlavor.equals(flavor);
        }

        @Override
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
            return listOfFiles;
        }
    }

}

In my tests, I was able to place a File in the List, wrap into a Transferable, pass it to the Clipboard and was able to paste the file through the system (Windows Explorer)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks, great answer, but this is only the first part of a problem I'm trying to solve, see my next related question. – Frank Aug 04 '15 at 02:06
  • I wonder if it's possible to paste copied file from clipboard in Java? – Артур Гудиев Aug 07 '18 at 11:53
  • @АртурГудиев I’m not sure what you mean - do you to “copy” a File from Java to the system clipboard and then paste through something like explorer or the other way round - the general answer is “yes”, you can – MadProgrammer Aug 07 '18 at 19:35
  • @АртурГудиев Sure [something like this example](https://stackoverflow.com/questions/22602950/is-it-possible-to-know-whether-the-copied-content-in-clipboard-is-mp3-file-using/22603155#22603155) demonstrates the basic idea of taking a `File` list from the clipboard, once you have the `File` reference you can do what ever you want – MadProgrammer Aug 08 '18 at 10:34