java.awt.datatransfer.Clipboard剪切板获取String字符串文本
有两种方法获取
-
直接从Clipboard获得
(String) systemClipboard.getData(DataFlavor.stringFlavor);
-
从Clipboard获得Transable再获得String
(String) systemClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);
Clipboard 的 getData(DataFlavor flavor)
的源码, java1.5版开始才有
public Object getData(DataFlavor flavor)throws UnsupportedFlavorException, IOException {if (flavor == null) {throw new NullPointerException("flavor");}Transferable cntnts = getContents(null);if (cntnts == null) {throw new UnsupportedFlavorException(flavor);}return cntnts.getTransferData(flavor);}
包装的获取剪切板文本的方法,没有判断内容是否是String
public static String getSystemClipboardString(Clipboard systemClipboard) {String result = null;if(systemClipboard==null)systemClipboard=Toolkit.getDefaultToolkit().getSystemClipboard();try {// result = (String) systemClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);// ↑都可↓ 下面的getData方法1.5版才有result = (String) systemClipboard.getData(DataFlavor.stringFlavor);} catch (UnsupportedFlavorException | IOException e) {e.printStackTrace();}return result;}public static String getSystemClipboardString() {return getSystemClipboardString(null);}
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;public class AwtClipboard获取剪切板文本的方法 {public static String getSystemClipboardString(Clipboard systemClipboard) {String result = null;if(systemClipboard==null)systemClipboard=Toolkit.getDefaultToolkit().getSystemClipboard();try {// result = (String) systemClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);// ↑都可↓ 下面的getData方法1.5版才有result = (String) systemClipboard.getData(DataFlavor.stringFlavor);} catch (UnsupportedFlavorException | IOException e) {e.printStackTrace();}return result;}public static String getSystemClipboardString() {return getSystemClipboardString(null);}public static void main(String...arguments) {System.out.println(getSystemClipboardString());}}
判断剪切板Transferable内容是否是String
- 方法1, 从clipboard获得transferable, 再用transferable的
isDataFlavorSupported(DataFlavor flavor)
方法判断
boolean 是否剪切板的内容是文本 = transferable.isDataFlavorSupported(DataFlavor.stringFlavor);
- 方法2 直接用clipboard的
isDataFlavorAvailable(DataFlavor flavor)
方法判断 , 该方法1.5版才有
boolean 是否剪切板的内容是文本 = systemClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor);
Clipboard的isDataFlavorAvailable(DataFlavor flavor)
方法从1.5版本开始才有, 可以简化代码, 其实质也是获得Transferable实例后调用其isDataFlavorSupported(DataFlavor flavor)
方法判断
源码如下
/*** Returns whether or not the current contents of this clipboard can be* provided in the specified {@code DataFlavor}.** @param flavor the requested {@code DataFlavor} for the contents* @return {@code true} if the current contents of this clipboard can be* provided in the specified {@code DataFlavor}; {@code false}* otherwise* @throws NullPointerException if {@code flavor} is {@code null}* @throws IllegalStateException if this clipboard is currently unavailable* @since 1.5*/public boolean isDataFlavorAvailable(DataFlavor flavor) {if (flavor == null) {throw new NullPointerException("flavor");}Transferable cntnts = getContents(null);if (cntnts == null) {return false;}return cntnts.isDataFlavorSupported(flavor);}
包装的获取剪切板文本的方法,且判断内容是否是String
public static String getSystemClipboardString(Clipboard systemClipboard) {String result = "剪切板的内容不是字符串文本StringText";if(systemClipboard==null)systemClipboard=Toolkit.getDefaultToolkit().getSystemClipboard();// boolean 剪切板的内容是文本 = systemClipboard.getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor);boolean 剪切板的内容是文本 = systemClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor); // 1.5版才有if(剪切板的内容是文本) {try {// result = (String) systemClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);result = (String) systemClipboard.getData(DataFlavor.stringFlavor); // 1.5版才有} catch (UnsupportedFlavorException | IOException e) {e.printStackTrace();}}return result;}public static String getSystemClipboardString() {return getSystemClipboardString(null);}
package 剪切板复制粘贴;import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.io.IOException;public class 判断剪切板的内容是文本才获取 {public static String getSystemClipboardString(Clipboard systemClipboard) {String result = "剪切板的内容不是字符串文本StringText";if(systemClipboard==null)systemClipboard=Toolkit.getDefaultToolkit().getSystemClipboard();// boolean 剪切板的内容是文本 = systemClipboard.getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor);boolean 剪切板的内容是文本 = systemClipboard.isDataFlavorAvailable(DataFlavor.stringFlavor); // 1.5版才有if(剪切板的内容是文本) {try {// result = (String) systemClipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);result = (String) systemClipboard.getData(DataFlavor.stringFlavor); // 1.5版才有} catch (UnsupportedFlavorException | IOException e) {e.printStackTrace();}}return result;}public static String getSystemClipboardString() {return getSystemClipboardString(null);}public static void main (String...arguments)throws Exception{System.out.println(getSystemClipboardString());}}