-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyHistory.java
More file actions
101 lines (83 loc) · 3.17 KB
/
Copy pathCopyHistory.java
File metadata and controls
101 lines (83 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
CLIPBOARD HISTORY
This program will monitor the content of the clipboard and save it to a file
That way we'll be able to see evrything we have copied and when we have copied them
*/
package copyhistory;
/**
*
* @author lamine
*/
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.util.ArrayList;
import java.io.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CopyHistory {
/**
* @param args the command line arguments
*/
private static String time;
public static void main(String[] args) {
saveClipData();
}
// get text in the clipboard
public static String getClipText(){
String clipContent = "";
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable content = cb.getContents(null);
if(content != null && content.isDataFlavorSupported(DataFlavor.stringFlavor)){
try{
clipContent = String.valueOf(content.getTransferData(DataFlavor.stringFlavor));
}catch(Exception e){
System.out.println(e.getMessage());
}
}
return clipContent;
}
public static void saveClipData(){
ArrayList<String> copies = new ArrayList<>();
String file = "cliphistory.txt";
String logFile = "log_cliphistory.txt";
PrintWriter writer = null;
int i = 0;
while(true){
try{
String text = getClipText();
time = new SimpleDateFormat("yyyy/MM/dd HH:mm").format(Calendar.getInstance().getTime());
if(!copies.contains(text)){
copies.add(text);
writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
writer.println(time+" | "+text);
Thread.sleep(30000);
}
// clear the list every 100 copies
if(copies.size() >= 100){
String lastCopied = copies.get(copies.size()-1); // get the last element in the arraylist
copies.clear();
copies.add(lastCopied); // add the last element in the list to avoid duplicates
//throw new IllegalArgumentException("ohhh snap!"); // test of the log by throwing an exception ;)
}
}catch(Exception e){
System.out.println(e.getMessage());
try {
PrintWriter w = new PrintWriter(new BufferedWriter(new FileWriter(logFile, true)));
w.println(time + "|" + e.getMessage());
w.close();
} catch (IOException ex) {
Logger.getLogger(ClipboardWatcher.class.getName()).log(Level.SEVERE, null, ex);
}
}finally{
writer.close();
}
i++;
}
}
}