import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.JOptionPane;
public class AboutClipboard {
	
	public static void main(String[] args) throws UnsupportedFlavorException, IOException {
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();//得到系统的剪贴板 
		Transferable clipData = clipboard.getContents(clipboard); //读取剪贴板里的内容
	
		try
		{
			String s = (String)(clipData.getTransferData(DataFlavor.stringFlavor)); //变成字符串 
			System.out.print(s);//打印出剪贴板里的内容
		}catch(IOException VariableDeclaratorId){}
		//保存数据到剪贴板
		String selection = JOptionPane.showInputDialog("输入的信息将存储到剪贴板中");
		Clipboard clipboard1 = Toolkit.getDefaultToolkit().getSystemClipboard(); 
		StringSelection data = new StringSelection(selection); 
		clipboard1.setContents (data, data); //setContent
	}

}