简体   繁体   中英

Copy byte array directly into clipboard in Java

Is there any workaround to directly copy byte[] into clipboard.

The following code works fine for String.

String str = "My - String";
StringSelection stringSelection = new StringSelection(str);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);

The reason is my byte[] is lengthy and it will take memory and time to convert it into String, as if I use new String(bArray) etc before copying it.

Thanks.

You can do this with a DataHandler :

DataHandler dataHandler = new DataHandler(bytes, "application/octet-stream");
clpbrd.setContents(dataHandler, null);

DataHandler is even capable of handling input streams, so you might not need the byte array at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM