java读写剪切板
package top.lishuoboy.java_basic.clip_board;
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.IOException;
/**
* @Author top.lishuoboy
* @Date 2019/10/10 14:21
* @Version 1.0
*/
public class ClipboardTest {
public static void main(String[] args) throws IOException, UnsupportedFlavorException {
setIntoClipboard("aaa");
System.err.println(getFromClipboard());
}
public static void setIntoClipboard(String data) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(data), null);
}
public static String getFromClipboard() throws IOException, UnsupportedFlavorException {
Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
String result = "";
if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
result = (String) transferable.getTransferData(DataFlavor.stringFlavor);
}
return result;
}
}