import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.mapred.JobConf; // this example is testing for static valiable value // it will show the value of static valiable is related to Class, not object class testb { // object variable public void read() { System.out.println(test.sna); } } class Fu { T go; void setFu(T go) { this.go = go; } T getFu() { return go; } } class animal { String head = "head"; String feet = "feet"; String body = "body"; String hand = "head"; String mind = "mind"; int id = 1234567; } class bird extends animal { String feather = "feather"; bird() { hand = "wing"; id = 1234568; System.out.println(head + hand + body + feet + "id=" + id); System.out.println("super = " + super.hand + "this = " + this.hand); } } class dog extends animal { dog(animal a) { System.out.println(a.head + hand + body + feet + "id=" + id); } void setbird(bird b) { System.out.println(b.head + hand + body + feet + "id=" + id); } } public class test { int na; static int sna; static String str; public test() { } public test(String st) { str = st; } public test(int a, int b, String st) { na = a; sna = b; str = st; } public void print(String str) { System.out.println(str); } @SuppressWarnings( { "static-access" }) public void strToken() throws IOException { test a = new test(1, 2, "a"); a.print("a.na = " + a.na); a.print("test.sna = " + test.sna); a.print("a.sna = " + a.sna); // warning about it's not a regular // accessing method test b = new test(3, 4, "a"); b.print("b.na = " + b.na); b.print("test.sna = " + test.sna); b.print("b.sna = " + b.sna); // warning about it's not a regular // accessing method a.print("a.sna = " + a.sna); // a.sna = test.sna -> b.sna String tmp[] = test.str.split(":"); String Column_Family = tmp[0] + ":"; a.print("column family = " + Column_Family); a.print("ori str = " + test.str); // test fileoutputstream FileOutputStream out = new FileOutputStream(new File( "/home/waue/mr-result.txt")); out.write("hello".getBytes()); out.close(); // test randomaccessfile RandomAccessFile raf = new RandomAccessFile("/home/waue/mr-result.txt", "rw"); raf.seek(raf.length()); // move pointer to end raf.write("\n go \t ahead".getBytes()); raf.close(); // test StringTokenizer StringTokenizer st = new StringTokenizer("this is a test"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } String parseFirstLine(String in, String ou) throws IOException { BufferedReader fi = new BufferedReader(new FileReader(new File(in))); BufferedWriter fw = new BufferedWriter(new FileWriter(new File(ou))); String first_line, data; first_line = fi.readLine(); do { data = fi.readLine(); if (data == null) { break; } else { fw.write(data + "\n"); } } while (true); fw.close(); return first_line; } boolean deleteFile(String str) throws IOException { File ttt = new File(str); if (ttt.exists()) { if (!ttt.delete()) { System.err.print("delete file error !"); } } else { System.out.println("file not exit!"); } return true; } void Inherbit() { animal a = new animal(); bird b = new bird(); dog d = new dog(b); animal aaa = new bird(); // dog ddd = new animal(); -> error animal aa = (animal) b; System.out.println(aa.hand); System.out.println(aa.mind); // bird bb = (bird) a; -> error // d.setbird(aa); -> error // System.out.println(aa.feather); -> error // Fu Fu ak; ak = new Fu(); ak.getFu(); Fu in = ak; // in.setFu("ss"); -> error in.getFu(); } void ttt() throws IOException { Path pi; JobConf conf = new JobConf(test.class); FileStatus[] fi = FileSystem.get(conf).listStatus( new Path("/user/waue/input/")); for (int i = 0; i < fi.length; i++) { pi = fi[i].getPath(); System.out.println(pi.getName()); } } void parseN() throws IOException { BufferedReader br = new BufferedReader(new FileReader(new File( "/home/waue/Desktop/a"))); // BufferedWriter bw = new BufferedWriter(new FileWriter(new // File("/home/waue/Desktop/b"))); String line = br.readLine(); // char n = '\n'; int Count = 0; do { if (line.isEmpty()) { System.out.println("empty !"); } else { System.out.println(line); } line = br.readLine(); Count++; } while (line != null); } void regular() { Pattern patten_line; Matcher matcher; String logData = new String(); String line = "[**] [1:2003:8] MS-SQL Worm propagation attempt [**]"; // String line = "[**] [1:2189:3] BAD-TRAFFIC IP Proto 103 PIM [**]"; patten_line = Pattern .compile("^\\[\\**\\] \\[([0-9]*):([0-9]*):([0-9]*)\\] ([^\\[]*)\\[\\**\\]$"); matcher = patten_line.matcher(line); System.out.println("go"); if (matcher.matches()) { int number = matcher.groupCount(); String[] data = new String[number]; for (int j = 0; j < number; j++) { data[j] = matcher.group(j + 1); logData += (data[j] + ";"); } System.out.println(logData); } System.out.println("end"); } void enumTest() { for (EnumTest t : EnumTest.values()) { System.out.println(t); } } void Date() throws Exception { String da = "10/Jul/2008:08:26:55"; SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.US); Date timestamp = sdf.parse(da); long timeint = timestamp.getTime(); System.out.println(timestamp.toString()); System.out.println(timeint); } void getNowTime() { SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); Date date = new Date(); String strDate = sdFormat.format(date); System.out.println(strDate); System.out.println(date.getTime()); } void setTime() { String str = "07/08:01:01:01"; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM:HH:mm:ss", Locale.TAIWAN); Date tim = sdf.parse(str, new ParsePosition(0)); System.out.println(tim); Long timestamp = tim.getTime(); System.out.println(timestamp); } void selRegular(String str) { Pattern patten_line1, patten_line2; Matcher matcher; String logData = new String(); patten_line1 = Pattern .compile("^\\[Classification: ([^\\]]*)\\] \\[Priority: ([1-9]*)\\].*$"); patten_line2 = Pattern.compile("^\\[Priority: ([1-9]*)\\].*$"); matcher = patten_line1.matcher(str); if (matcher.matches()) { int number = matcher.groupCount(); String[] data = new String[number]; for (int j = 0; j < number; j++) { data[j] = matcher.group(j + 1); logData += (data[j] + ";"); } } else { matcher = patten_line2.matcher(str); if (matcher.matches()) { logData = "Port Scan;" + matcher.group(1) + ";"; } else { logData = ";;"; } } System.out.println(logData); System.out.println("end"); } void splitExc(){ String arr = "140.110.1.1:255"; int n = arr.indexOf(":"); if (n == -1) { System.out.println("ip :" + arr); } else { System.out.println("ok"); String[] vec = arr.split(":"); System.out.println("ip :" + vec[0]); System.out.println("port :" + vec[1]); } } void callByRefFun(int a,int b){ b = a; } void callByRef(){ test t = new test(); this.na =5; int b = 10; t.callByRefFun(b, this.na); System.out.println(this.na); } public static void main(String[] args) throws Exception { test t = new test(); // 測試 strToken函數 // t.strToken(); // 測試 檔案io 與 解析 // System.out.println(a.parseFirstLine("/home/waue/test.txt", // "/home/waue/out.tmp.txt")); // 測試 listStatus // t.ttt(); // 測試 繼承 與 汎型 // t.Inherbit() // 測試 "換行" 是否為 "\n" // t.parseN(); // 測試 正規表示法 // t.regular(); // 測試 enum 列舉 // t.enumTest(); // 測試設定 // t.Date(); // 測試取得現在時間 // t.getNowTime(); // t.setTime(); // 測試 選擇性正規表示法 // t.selRegular(String str); // 測試 split()的例外處理 // t.splitExc(); t.callByRef(); // if(vec[1] != null){ // System.out.print(" \nport:" + vec[1] ); // } } } enum EnumTest { R, L, GO }