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

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

update some new ..

File size: 4.4 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.util.StringTokenizer;
10
11import org.apache.hadoop.fs.FileStatus;
12import org.apache.hadoop.fs.FileSystem;
13import org.apache.hadoop.fs.Path;
14import org.apache.hadoop.mapred.JobConf;
15
16
17// this example is testing for static valiable value
18// it will show the value of static valiable is related to Class, not object
19
20class testb {
21  // object variable
22  public void read() {
23    System.out.println(test.sna);
24  }
25}
26
27public class test {
28  int na;
29  static int sna;
30  static String str;
31  public test() {
32  }
33
34  public test(String st) {
35    str = st;
36  }
37
38  public test(int a, int b, String st) {
39    na = a;
40    sna = b;
41    str = st;
42  }
43
44  public void print(String str) {
45    System.out.println(str);
46  }
47
48  @SuppressWarnings( { "static-access" })
49  public void strToken() throws IOException {
50    test a = new test(1, 2, "a");
51    a.print("a.na = " + a.na);
52    a.print("test.sna = " + test.sna);
53
54    a.print("a.sna = " + a.sna); // warning about it's not a regular
55    // accessing method
56
57    test b = new test(3, 4, "a");
58    b.print("b.na = " + b.na);
59    b.print("test.sna = " + test.sna);
60    b.print("b.sna = " + b.sna); // warning about it's not a regular
61    // accessing method
62
63    a.print("a.sna = " + a.sna); // a.sna = test.sna -> b.sna
64    String tmp[] = test.str.split(":");
65    String Column_Family = tmp[0] + ":";
66    a.print("column family = " + Column_Family);
67    a.print("ori str = " + test.str);
68    // test fileoutputstream
69    FileOutputStream out = new FileOutputStream(new File(
70        "/home/waue/mr-result.txt"));
71    out.write("hello".getBytes());
72    out.close();
73    // test randomaccessfile
74    RandomAccessFile raf = new RandomAccessFile("/home/waue/mr-result.txt",
75        "rw");
76    raf.seek(raf.length()); // move pointer to end
77    raf.write("\n go \t ahead".getBytes());
78    raf.close();
79    // test StringTokenizer
80    StringTokenizer st = new StringTokenizer("this is a test");
81    while (st.hasMoreTokens()) {
82      System.out.println(st.nextToken());
83    }
84  }
85
86  String parseFirstLine(String in, String ou) throws IOException {
87    BufferedReader fi = new BufferedReader(new FileReader(new File(in)));
88    BufferedWriter fw = new BufferedWriter(new FileWriter(new File(ou)));
89    String first_line, data;
90    first_line = fi.readLine();
91    do {
92      data = fi.readLine();
93      if (data == null) {
94        break;
95      } else {
96        fw.write(data + "\n");
97      }
98    } while (true);
99    fw.close();
100    return first_line;
101  }
102
103  boolean deleteFile(String str) throws IOException {
104    File ttt = new File(str);
105    if (ttt.exists()) {
106      if (!ttt.delete()) {
107        System.err.print("delete file error !");
108      }
109    } else {
110      System.out.println("file not exit!");
111    }
112    return true;
113  }
114
115  void ttt() throws IOException {
116    Path pi;
117    JobConf conf = new JobConf(test.class);
118    FileStatus[] fi = FileSystem.get(conf).listStatus(
119        new Path("/user/waue/input/"));
120    for (int i = 0; i < fi.length; i++) {
121      pi = fi[i].getPath();
122      System.out.println(pi.getName());
123    }
124  }
125
126  public static void main(String[] args) throws IOException {
127    // test a = new test();
128    // a.strToken();
129    // System.out.println(a.parseFirstLine("/home/waue/test.txt",
130    // "/home/waue/out.tmp.txt"));
131    // ttt();
132   
133   
134    animal a = new animal();
135    bird b = new bird();
136    dog d = new dog(b);
137    animal aaa = new bird();
138//    dog ddd = new animal();  -> error
139   
140    animal aa = (animal) b;
141    System.out.println(aa.hand);
142    System.out.println(aa.mind);   
143//    bird bb = (bird) a; -> error
144//    d.setbird(aa); -> error
145//    System.out.println(aa.feather); -> error
146
147    // Fu <T>
148    Fu<? extends String> ak;
149    ak = new Fu<String>();
150    ak.getFu();
151    Fu<?> in = ak;
152//    in.setFu("ss"); -> error
153    in.getFu();
154  }
155}
156class Fu <T>{
157  T go;
158  void setFu(T go){
159    this.go = go;
160  }
161  T getFu(){
162    return go;
163  }
164}
165
166class animal{
167  String head = "head";
168  String feet = "feet";
169  String body = "body";
170  String hand = "head";
171  String mind = "mind";
172  int id = 1234567;
173}
174class bird extends animal{
175  String feather = "feather";
176  bird(){
177    hand = "wing";
178    id = 1234568;
179    System.out.println(head + hand + body + feet + "id="+id);
180    System.out.println("super = " + super.hand +"this = " + this.hand);
181  }
182}
183class dog extends animal{
184  dog(animal a){
185    System.out.println(a.head + hand + body + feet + "id="+id);
186  }
187  void setbird(bird b){
188    System.out.println(b.head + hand + body + feet + "id="+id);
189  }
190}
Note: See TracBrowser for help on using the repository browser.