test类:提示用户程序已启动,提示保存位置,清空剪切板。
package com.ariya.service;
import com.ariya.service.impl.ClipboardServiceImpl;
/**
* @author Ariya
* 程序入口
*/
public class Test {
public static void main(String[] args) {
ClipboardServiceImpl clipboardService =new ClipboardServiceImpl();
System.out.println("程序已启动:》》》》》》》》");
System.out.println("请开始复制:》》》》》》》》");
System.out.println("默认保存位置C:/doc");
clipboardService.setClipboardText("");
clipboardService.save2txt(null);
}
}
接口类:
package com.ariya.service;
/**
* @author Ariya
*/
public interface IClipboardService {
/**
* 按入参地址保存到txt文件
* @param filePath
*/
void save2txt(String filePath);
/**
* 把剪切板的内容返回
* @return String
*/
StringgetClipboardText();
/**
* 向剪切板写东西
* @param text
*/
void setClipboardText(String text);
}
实现类:
package com.ariya.service.impl;
import com.ariya.service.IClipboardService;
import org.apache.commons.lang3.StringUtils;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
/**
* @author Ariya
*/
public class ClipboardServiceImplimplements IClipboardService {
StringoldText =null;
StringnewText =null;
@Override
public void save2txt(String path) {
String filePath =null;
if(StringUtils.isEmpty(path) || path =="1") {
path ="C:/doc";
File file =new File(path);
if(!file.exists()) {
file.mkdir();
}
}
filePath = path +"/text.txt";
//持续读取
while(true) {
String clipboardText = getClipboardText();
//读到就写
if(!StringUtils.isEmpty(clipboardText)) {
String charsetName ="UTF-8";
try (OutputStreamWriter osw =new OutputStreamWriter(new FileOutputStream(filePath, true), charsetName)) {
//写操作
osw.write(clipboardText);
osw.write("\n");
osw.flush();
System.out.println("("+ clipboardText +")>>>“存盘成功");
}catch (Exception e) {
e.printStackTrace();
}
}
//睡眠0.2秒
try {
Thread.sleep(200);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public StringgetClipboardText() {
//获取剪切板对象
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//获取剪切板中的可转换对象
Transferable transferable =null;
try {
//这里很容易抛各种异常
transferable = clipboard.getContents(null);
}catch (IllegalArgumentException e) {
System.out.println("无法复制保存该内容");
setClipboardText("");
return null;
}catch (Exception e) {
System.out.println("系统错误");
setClipboardText("");
}
//如果可转换对象不是null
if (transferable !=null) {
// 检查内容是否是文本类型
if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
//把文本类型可转对象直接转换为string
newText = (String) transferable.getTransferData(DataFlavor.stringFlavor);
//如果和之前的相等,直接return
if(newText.equals(oldText)) {
return null;
}
//否则就把新字符串赋值给老字符串,然后return出去
oldText =newText;
return newText;
}catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
public void setClipboardText(String text) {
//获取剪切板对象
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//实例化可转换对象
Transferable transferable =new StringSelection(text);
//往剪切板写入可转换对象
clipboard.setContents(transferable, null);
}
}
pom文件:主要是用于打包jar,实现jar的自启动
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.ariya
clipboard-copy
1.0-SNAPSHOT
org.apache.commons
commons-lang3
3.11
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
org.springframework.boot
spring-boot-maven-plugin
com.ariya.service.Test
JAR
true
repackage