69

I already know how to get plain text from the clipboard in Java, but sometimes the text is encoded in some weird DataFlavor, like when copying from Microsoft Word or from a website or even source code from Eclipse.

How to extract pure plain text from these DataFlavors?

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
clamp
  • 33,000
  • 75
  • 203
  • 299

3 Answers3

80
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

String data = (String) Toolkit.getDefaultToolkit()
                .getSystemClipboard().getData(DataFlavor.stringFlavor); 

with the getData() Method and the stringFlavor you should get plain text from the clipboard.

If there are weird text in the clipboard, I think, this should be a problem of the program which puts the data in the clipboard.

null_override
  • 467
  • 3
  • 10
  • 30
Dragon8
  • 1,775
  • 12
  • 8
  • 4
    WARNING: Using the above code you can hit this known Java Bug http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6606476 (as I did) and there is no work around. Fortunately I have Perl installed and so I can invoke a Perl script to set the clipboard contents to plain text. :( – Steve Waring Jun 20 '15 at 15:15
  • 1
    @SteveWaring thanks for the heads up on that odd uncatchable error. Haven't tested this yet, but I'd suggest setting the System.err stream to null and catching null pointers when attempting Dragon8's solution. (Of course don't forget to set it back to normal when done ^_^) – NekoKikoushi Oct 06 '17 at 17:01
  • is there a way to do this in headless mode browsers? – Vrushank Doshi Nov 24 '20 at 04:04
3

You can use following method getting clipboard text in Java:

public String getClipBoard(){
    try {
        return (String)Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
    } catch (HeadlessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();            
    } catch (UnsupportedFlavorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();            
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "";
}
Christoph
  • 50,121
  • 21
  • 99
  • 128
Mihir Patel
  • 404
  • 6
  • 14
0

First I haven't worked with clipboard but this seems intresting

From http://docstore.mik.ua/orelly/java/awt/ch16_01.htm

"To read data from the clipboard, a program calls the Transferable.getTransferData() method. If the data is represented by a DataFlavor that doesn't correspond to a Java class (for example, plainTextFlavor), getTransferData() returns an InputStream for you to read the data from."

So if you give it a class which doesn't correspont you get the InputStream and then you can read the "pure" text from the InputStream yourself.

  • 2
    [DataFlavor.plainTextFlavor](http://docs.oracle.com/javase/7/docs/api/java/awt/datatransfer/DataFlavor.html#plainTextFlavor) is deprecated since Java 1.3. – Huxi Nov 14 '13 at 12:32