仅从剪贴板获取可读文本 [英] Get readable text only from clipboard

查看:106
本文介绍了仅从剪贴板获取可读文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经知道如何从Java中的剪贴板中获取纯文本,但有时文本被编码为一些奇怪的 DataFlavor ,就像从Microsoft Word或从来自Eclipse的网站甚至源代码。

I already know how to get plain text from the clipboard in Java, but sometimes the text is encoded in some weird DataFlavor, like when copying from Microsoft Word or from a website or even source code from Eclipse.

如何从这些 DataFlavor 中提取纯纯文本?

How to extract pure plain text from these DataFlavors?

推荐答案

import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

String data = (String) Toolkit.getDefaultToolkit()
                .getSystemClipboard().getData(DataFlavor.stringFlavor); 

使用getData()方法和stringFlavor你应该从剪贴板中获得纯文本。

with the getData() Method and the stringFlavor you should get plain Text from the clipboard.

如果剪贴板中有奇怪的文字,我想,这应该是将数据放入剪贴板的程序问题。

if there are weird text in the clipboard, i think, this should a problem of the programm which puts the data in the clipboard.

这篇关于仅从剪贴板获取可读文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆