Java Cmdline Random String Marker


此程式主要是為了方便自己而編寫的
同時亦都順道學習一下 Java,主要最難的部份是複製文字..
而此程式用了 Cmdline 的方式進制操作,比起上一版的 Java GUI 感覺方便很多..

至於編譯的方法請自行到查找吧,程式可能有不足之處 @@

公開一下代碼 :

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Author : Zeuxis Lo (傻心 - 窮等人家)
* P-Name : Random String Marker
* Version: 0.001 Beta
* WebSite: http://skhk.uni.cc/
* Date : 2007/10/10 22:52
*
* 未經同意,請必私下轉載
* 請不要用於商業用途,只提供私人研究之用
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;

class RandomStringMarker {
final static String VER = "v 0.001 Beta";

public static void main(String[] args) throws IOException {
String tmp = "";

RandomMarker rm = new RandomMarker();
CopyToClipboard ctc = new CopyToClipboard();
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Command : \n");
System.out.println("random\t- Mark Random String");
System.out.println("exit\t- Exit Programme");
System.out.println("version\t- Show Programme Version\n");

while(true) {
if (tmp.equals("exit")) {
System.out.println("\nThanks for your use !");
out();
}else if (tmp.equals("version")) {
System.out.println("\nAuthor\t: Zeuxis Lo");
System.out.println("P-Name\t: Random String Marker");
System.out.println("Verison\t: " + VER);
System.out.println("WebSite\t: http://skhk.uni.cc/");
out();
}else if (tmp.equals("random")) {

String want = "";

while(true) {

if (want.equals("y")) {
ctc.toClipboard(rm.rndStr);
System.out.println("\nString Saved ! Thanks for you use !");
out();
}else if (want.equals("n")) {
System.out.print("Do you want to random a new string [y/n] : ");
String n_want = buf.readLine();

if (n_want.equals("n")) {
System.out.println("\nThanks for you use !");
out();
}else{
want = "";
}

}else{
System.out.print("Please Random String Size : ");
String size = buf.readLine();

System.out.println("Random String Is : " + rm.randStr(Integer.parseInt(size)));

System.out.print("Do you want to save it [y/n] : ");
want = buf.readLine();
}

}

}else{
System.out.print("Please Enter Command : ");
tmp = buf.readLine();
}
}
}

private static void out() {
System.exit(1);
}
}

//
class RandomMarker {
public static String rndStr;

public static String randStr(int num) {
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678";

StringBuffer saveStr = new StringBuffer();

for (int i=1; i<=num; i++) {
int numAt = (int)(Math.random()*(str.length()-1));
saveStr.append(str.charAt(numAt));
}

rndStr = saveStr.toString();

return rndStr;
}
}

//
class CopyToClipboard implements ClipboardOwner {
public void toClipboard(String rndStr) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkSystemClipboardAccess();
}catch (Exception e) {
e.printStackTrace();
}
}
Toolkit tk = Toolkit.getDefaultToolkit();
StringSelection st = new StringSelection(rndStr);
Clipboard cp = tk.getSystemClipboard();
cp.setContents(st, this);
}

public void lostOwnership(Clipboard clip, Transferable tr) {
System.out.println("Lost Clipboard Ownership?!?");
}
}