[24] | 1 | import java.io.BufferedReader; |
---|
| 2 | import java.io.BufferedWriter; |
---|
| 3 | import java.io.File; |
---|
| 4 | import java.io.FileOutputStream; |
---|
| 5 | import java.io.FileReader; |
---|
| 6 | import java.io.FileWriter; |
---|
| 7 | import java.io.IOException; |
---|
| 8 | import java.io.RandomAccessFile; |
---|
[45] | 9 | import java.text.SimpleDateFormat; |
---|
| 10 | import java.util.Date; |
---|
| 11 | import java.util.Locale; |
---|
[24] | 12 | import java.util.StringTokenizer; |
---|
[42] | 13 | import java.util.regex.Matcher; |
---|
| 14 | import java.util.regex.Pattern; |
---|
[31] | 15 | |
---|
[24] | 16 | import org.apache.hadoop.fs.FileStatus; |
---|
| 17 | import org.apache.hadoop.fs.FileSystem; |
---|
| 18 | import org.apache.hadoop.fs.Path; |
---|
| 19 | import org.apache.hadoop.mapred.JobConf; |
---|
| 20 | |
---|
| 21 | // this example is testing for static valiable value |
---|
| 22 | // it will show the value of static valiable is related to Class, not object |
---|
| 23 | |
---|
| 24 | class testb { |
---|
| 25 | // object variable |
---|
| 26 | public void read() { |
---|
| 27 | System.out.println(test.sna); |
---|
| 28 | } |
---|
| 29 | } |
---|
| 30 | |
---|
[42] | 31 | class Fu<T> { |
---|
| 32 | T go; |
---|
| 33 | |
---|
| 34 | void setFu(T go) { |
---|
| 35 | this.go = go; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | T getFu() { |
---|
| 39 | return go; |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | class animal { |
---|
| 44 | String head = "head"; |
---|
| 45 | |
---|
| 46 | String feet = "feet"; |
---|
| 47 | |
---|
| 48 | String body = "body"; |
---|
| 49 | |
---|
| 50 | String hand = "head"; |
---|
| 51 | |
---|
| 52 | String mind = "mind"; |
---|
| 53 | |
---|
| 54 | int id = 1234567; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | class bird extends animal { |
---|
| 58 | String feather = "feather"; |
---|
| 59 | |
---|
| 60 | bird() { |
---|
| 61 | hand = "wing"; |
---|
| 62 | id = 1234568; |
---|
| 63 | System.out.println(head + hand + body + feet + "id=" + id); |
---|
| 64 | System.out.println("super = " + super.hand + "this = " + this.hand); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | class dog extends animal { |
---|
| 69 | dog(animal a) { |
---|
| 70 | System.out.println(a.head + hand + body + feet + "id=" + id); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void setbird(bird b) { |
---|
| 74 | System.out.println(b.head + hand + body + feet + "id=" + id); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
[24] | 78 | public class test { |
---|
| 79 | int na; |
---|
[42] | 80 | |
---|
[24] | 81 | static int sna; |
---|
[42] | 82 | |
---|
[24] | 83 | static String str; |
---|
[42] | 84 | |
---|
[24] | 85 | public test() { |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | public test(String st) { |
---|
| 89 | str = st; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | public test(int a, int b, String st) { |
---|
| 93 | na = a; |
---|
| 94 | sna = b; |
---|
| 95 | str = st; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | public void print(String str) { |
---|
| 99 | System.out.println(str); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | @SuppressWarnings( { "static-access" }) |
---|
| 103 | public void strToken() throws IOException { |
---|
| 104 | test a = new test(1, 2, "a"); |
---|
| 105 | a.print("a.na = " + a.na); |
---|
| 106 | a.print("test.sna = " + test.sna); |
---|
| 107 | |
---|
| 108 | a.print("a.sna = " + a.sna); // warning about it's not a regular |
---|
| 109 | // accessing method |
---|
| 110 | |
---|
| 111 | test b = new test(3, 4, "a"); |
---|
| 112 | b.print("b.na = " + b.na); |
---|
| 113 | b.print("test.sna = " + test.sna); |
---|
| 114 | b.print("b.sna = " + b.sna); // warning about it's not a regular |
---|
| 115 | // accessing method |
---|
| 116 | |
---|
| 117 | a.print("a.sna = " + a.sna); // a.sna = test.sna -> b.sna |
---|
| 118 | String tmp[] = test.str.split(":"); |
---|
| 119 | String Column_Family = tmp[0] + ":"; |
---|
| 120 | a.print("column family = " + Column_Family); |
---|
| 121 | a.print("ori str = " + test.str); |
---|
| 122 | // test fileoutputstream |
---|
| 123 | FileOutputStream out = new FileOutputStream(new File( |
---|
| 124 | "/home/waue/mr-result.txt")); |
---|
| 125 | out.write("hello".getBytes()); |
---|
| 126 | out.close(); |
---|
| 127 | // test randomaccessfile |
---|
| 128 | RandomAccessFile raf = new RandomAccessFile("/home/waue/mr-result.txt", |
---|
| 129 | "rw"); |
---|
| 130 | raf.seek(raf.length()); // move pointer to end |
---|
| 131 | raf.write("\n go \t ahead".getBytes()); |
---|
| 132 | raf.close(); |
---|
| 133 | // test StringTokenizer |
---|
| 134 | StringTokenizer st = new StringTokenizer("this is a test"); |
---|
| 135 | while (st.hasMoreTokens()) { |
---|
| 136 | System.out.println(st.nextToken()); |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | String parseFirstLine(String in, String ou) throws IOException { |
---|
| 141 | BufferedReader fi = new BufferedReader(new FileReader(new File(in))); |
---|
| 142 | BufferedWriter fw = new BufferedWriter(new FileWriter(new File(ou))); |
---|
| 143 | String first_line, data; |
---|
| 144 | first_line = fi.readLine(); |
---|
| 145 | do { |
---|
| 146 | data = fi.readLine(); |
---|
| 147 | if (data == null) { |
---|
| 148 | break; |
---|
| 149 | } else { |
---|
| 150 | fw.write(data + "\n"); |
---|
| 151 | } |
---|
| 152 | } while (true); |
---|
| 153 | fw.close(); |
---|
| 154 | return first_line; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | boolean deleteFile(String str) throws IOException { |
---|
| 158 | File ttt = new File(str); |
---|
| 159 | if (ttt.exists()) { |
---|
| 160 | if (!ttt.delete()) { |
---|
| 161 | System.err.print("delete file error !"); |
---|
| 162 | } |
---|
| 163 | } else { |
---|
| 164 | System.out.println("file not exit!"); |
---|
| 165 | } |
---|
| 166 | return true; |
---|
| 167 | } |
---|
| 168 | |
---|
[42] | 169 | void Inherbit() { |
---|
[31] | 170 | animal a = new animal(); |
---|
| 171 | bird b = new bird(); |
---|
| 172 | dog d = new dog(b); |
---|
| 173 | animal aaa = new bird(); |
---|
[42] | 174 | // dog ddd = new animal(); -> error |
---|
| 175 | |
---|
[31] | 176 | animal aa = (animal) b; |
---|
| 177 | System.out.println(aa.hand); |
---|
[42] | 178 | System.out.println(aa.mind); |
---|
| 179 | // bird bb = (bird) a; -> error |
---|
| 180 | // d.setbird(aa); -> error |
---|
| 181 | // System.out.println(aa.feather); -> error |
---|
[24] | 182 | |
---|
[31] | 183 | // Fu <T> |
---|
| 184 | Fu<? extends String> ak; |
---|
| 185 | ak = new Fu<String>(); |
---|
| 186 | ak.getFu(); |
---|
| 187 | Fu<?> in = ak; |
---|
[42] | 188 | // in.setFu("ss"); -> error |
---|
[31] | 189 | in.getFu(); |
---|
| 190 | } |
---|
[42] | 191 | |
---|
| 192 | void ttt() throws IOException { |
---|
| 193 | Path pi; |
---|
| 194 | JobConf conf = new JobConf(test.class); |
---|
| 195 | FileStatus[] fi = FileSystem.get(conf).listStatus( |
---|
| 196 | new Path("/user/waue/input/")); |
---|
| 197 | for (int i = 0; i < fi.length; i++) { |
---|
| 198 | pi = fi[i].getPath(); |
---|
| 199 | System.out.println(pi.getName()); |
---|
| 200 | } |
---|
[31] | 201 | } |
---|
[42] | 202 | |
---|
| 203 | void parseN() throws IOException { |
---|
| 204 | BufferedReader br = new BufferedReader(new FileReader(new File( |
---|
| 205 | "/home/waue/Desktop/a"))); |
---|
| 206 | // BufferedWriter bw = new BufferedWriter(new FileWriter(new |
---|
| 207 | // File("/home/waue/Desktop/b"))); |
---|
| 208 | String line = br.readLine(); |
---|
| 209 | // char n = '\n'; |
---|
| 210 | int Count = 0; |
---|
| 211 | do { |
---|
| 212 | if (line.isEmpty()) { |
---|
| 213 | System.out.println("empty !"); |
---|
| 214 | } else { |
---|
| 215 | System.out.println(line); |
---|
| 216 | } |
---|
| 217 | line = br.readLine(); |
---|
| 218 | Count++; |
---|
| 219 | } while (line != null); |
---|
| 220 | |
---|
[31] | 221 | } |
---|
[24] | 222 | |
---|
[42] | 223 | void regular() { |
---|
| 224 | Pattern patten_line; |
---|
| 225 | Matcher matcher; |
---|
| 226 | String logData = new String(); |
---|
| 227 | String line = "[**] [1:2003:8] MS-SQL Worm propagation attempt [**]"; |
---|
| 228 | // String line = "[**] [1:2189:3] BAD-TRAFFIC IP Proto 103 PIM [**]"; |
---|
| 229 | patten_line = Pattern |
---|
| 230 | .compile("^\\[\\**\\] \\[([0-9]*):([0-9]*):([0-9]*)\\] ([^\\[]*)\\[\\**\\]$"); |
---|
| 231 | matcher = patten_line.matcher(line); |
---|
| 232 | System.out.println("go"); |
---|
| 233 | if (matcher.matches()) { |
---|
| 234 | int number = matcher.groupCount(); |
---|
| 235 | String[] data = new String[number]; |
---|
| 236 | for (int j = 0; j < number; j++) { |
---|
| 237 | data[j] = matcher.group(j + 1); |
---|
| 238 | logData += (data[j] + ";"); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | System.out.println(logData); |
---|
| 242 | } |
---|
| 243 | System.out.println("end"); |
---|
[24] | 244 | } |
---|
[42] | 245 | |
---|
[45] | 246 | void enumTest() { |
---|
| 247 | for (EnumTest t : EnumTest.values()) { |
---|
[42] | 248 | |
---|
[45] | 249 | System.out.println(t); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | void Date()throws Exception{ |
---|
| 255 | String da = "20/Jul/2008:08:26:55"; |
---|
| 256 | SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss",Locale.US); |
---|
| 257 | Date timestamp = sdf.parse(da); |
---|
| 258 | System.out.println(timestamp.toString()); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | public static void main(String[] args) throws Exception { |
---|
| 262 | |
---|
[42] | 263 | test t = new test(); |
---|
| 264 | |
---|
| 265 | // 測試 strToken函數 |
---|
| 266 | // t.strToken(); |
---|
| 267 | // 測試 檔案io 與 解析 |
---|
| 268 | // System.out.println(a.parseFirstLine("/home/waue/test.txt", |
---|
| 269 | // "/home/waue/out.tmp.txt")); |
---|
| 270 | // 測試 listStatus |
---|
| 271 | // t.ttt(); |
---|
| 272 | // 測試 繼承 與 汎型 |
---|
| 273 | // t.Inherbit() |
---|
| 274 | // 測試 "換行" 是否為 "\n" |
---|
| 275 | // t.parseN(); |
---|
| 276 | // 測試 正規表示法 |
---|
| 277 | // t.regular(); |
---|
[45] | 278 | // 測試 enum 列舉 |
---|
| 279 | // t.enumTest(); |
---|
| 280 | t.Date(); |
---|
[42] | 281 | } |
---|
| 282 | |
---|
[31] | 283 | } |
---|
[45] | 284 | |
---|
| 285 | enum EnumTest { |
---|
| 286 | R, L, GO |
---|
| 287 | } |
---|