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;
    }
}

 

读写图片参考  https://www.cnblogs.com/brant/p/8120294.html

posted on 2019-10-10 14:44  小石头小祖宗  阅读(8)  评论(0编辑  收藏  举报  来源

导航