java相关测试


          

代码:

public class javaTest {  
    static String path ="D:\\123.txt";  
    public static void HashMapTest(){  
        HashMap<String, String> list =new HashMap<String, String>();  
        list.put("123", "abc");  
        list.put("456", "def");  
          
        System.out.println("123="+list.get("123"));  
        System.out.println("456="+list.get("456"));  
        if(list.containsKey("123")){  
            System.out.println("have 123="+list.get("123"));  
        }else {  
            System.out.println("123 is null");  
        }  
        if(list.containsKey("111")){  
            System.out.println("have 111="+list.get("111"));  
        }else {  
            System.out.println("111 is null");  
        }  
    }  
    public static void test123(){  
            for(int i=0; i<5; i++ ){  
                byte[] a = new byte[i];  
                for(int j=0; j<i; j++){  
                    a[j] = (byte) j;  
                    System.out.print(" "+a[j] );  
                }  
            }  
    }  
  
    private static void deleteFile(){  
        File file = new File(path);  
        if(file.exists()){  
            file.delete();  
        }  
    }  
      
    private static void fileAppend(String data){  
        FileOutputStream os;  
        try {  
            os = new FileOutputStream(path, true);  
            os.write(data.getBytes());  
            os.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
      
    private static byte[] readFile(){  
        byte[] result = new byte[12];  
        try {  
            FileInputStream is = new FileInputStream(path);  
            is.read(result, 0, 6);  
            is.read(result, 6, 6);  
            is.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return result;  
    }  
      
    private static byte[] readFile(int offset, int len){  
        byte[] result = new byte[offset+len];  
        try {  
            FileInputStream is = new FileInputStream(path);  
            is.read(result, offset, len);  
            is.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return result;  
    }  
      
    private static void queueTest(){  
        byte[] a = {1,2,3};  
        byte[] b = {4,5,6};  
        byte[] c = {7,8,9};  
        byte[] d = {1,1,1};  
        byte[] e = {2,2,2};  
        Queue<byte[]> queue = new LinkedList<byte[]>();  
        queue.offer(a);  
        queue.offer(b);  
        queue.offer(c);  
        byte[] a1 = queue.poll();  
        System.out.println("a1.len="+a1.length);  
        printHexString("1", a1);  
        for(byte[] q:queue){  
            printHexString("all",q);  
        }  
        a1 = queue.poll();  
        printHexString("2",a1);  
        for(byte[] q:queue){  
            printHexString("all",q);  
        }  
        queue.offer(d);  
        queue.offer(e);  
        System.out.println("1size="+queue.size());  
        a1 = queue.poll();  
        System.out.println("2size="+queue.size());  
        printHexString("3",a1);  
        for(byte[] q:queue){  
            printHexString("all=",q);  
        }  
    }  
      
    public static void printHexString(String hint, byte[] b) {  
        System.out.print(hint+":");  
        for (int i = 0; i < b.length; i++) {  
            String hex = Integer.toHexString(b[i] & 0xFF);  
            if (hex.length() == 1)  
                hex = '0' + hex;  
            System.out.print("0x"+hex.toUpperCase() + " ");  
        }  
        System.out.print("\n");  
    }  
      
    public static void main(String[] args) {  
        HashMapTest();  
        deleteFile();  
        fileAppend("123456");  
        fileAppend("abcabc");  
        printHexString("read file", readFile());  
        printHexString("1 read file", readFile(0, 6));  
        printHexString("2 read file", readFile(6, 6));  
        queueTest();  
    }  
}  
结果:
123=abc  
456=def  
have 123=abc  
111 is null  
read file:0x31 0x32 0x33 0x34 0x35 0x36 0x61 0x62 0x63 0x61 0x62 0x63   
1 read file:0x31 0x32 0x33 0x34 0x35 0x36   
2 read file:0x00 0x00 0x00 0x00 0x00 0x00 0x31 0x32 0x33 0x34 0x35 0x36   
a1.len=3  
1:0x01 0x02 0x03   
all:0x04 0x05 0x06   
all:0x07 0x08 0x09   
2:0x04 0x05 0x06   
all:0x07 0x08 0x09   
1size=3  
2size=2  
3:0x07 0x08 0x09   
all=:0x01 0x01 0x01   
all=:0x02 0x02 0x02