source: sample/hadoop-0.16/test.java @ 42

Last change on this file since 42 was 42, checked in by waue, 16 years ago

..

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