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 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值