操作剪贴板的
不记得为啥需要这个功能了。。
主要就是操作Clipboard类
放出来,需要就看看了
package utils;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 剪贴板功能
* @author kael
*
*/
public class ClipboardUtils
{
/** 把文件放到剪贴板,然后就可按ctrl+v粘贴了 */
public static void setFile( String file )
{
_FileTransferable f = new _FileTransferable();
f.addFile(file);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(f, null);
}
}
class _FileTransferable implements Transferable
{
private DataFlavor[] dataFlavors = new DataFlavor[] { DataFlavor.javaFileListFlavor };
private List<File> _files = new ArrayList<File>();
public void addFile(String file)
{
_files.add(new File(file));
}
@Override
public DataFlavor[] getTransferDataFlavors()
{
return dataFlavors;
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor)
{
for (int i = 0; i < dataFlavors.length; i++) {
if (dataFlavors[i].equals(flavor)) {
return true;
}
}
return false;
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
return _files;
}
}