zoukankan      html  css  js  c++  java
  • java生成连续MAC地址

    有时候性能测试的时候需要用到设备的MAC地址,为避免MAC地址重复,那么就生成连续的MAC地址就不会重复了

    直接看代码吧

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.math.BigInteger;
    import java.util.regex.Pattern;
    
    public class ConsequentMAC {
    	public static String pattern = "[0-9a-fA-F]{12}";
    	public static String patternSn = "[0-9A-Z]{18}";
    	public static  void genPatchMAC (String initialMAC,int count,String filePath) {
    		String startMAC = initialMAC.replace(":", "");
    		boolean isMatch = Pattern.matches(pattern,startMAC);
    		if(isMatch == false) {
    			System.out.println(startMAC + "初始MAC地址非法");
    			System.exit(0);
    		}
    		try {
    			File file = new File(filePath);
    			FileWriter writer = new FileWriter(file, true);
    			BigInteger remoteMAC = new BigInteger(startMAC,16);
    			BigInteger increase = new BigInteger("1");
    			String resultMAC = "";
    			for(int i=0;i<count;i++) {
    				resultMAC = remoteMAC.toString(16).toUpperCase();
    				writer.write(formatMAC(resultMAC) + "
    ");
    				remoteMAC = remoteMAC.add(increase);
    			}
    			writer.close();
    		} catch (IOException e) {			
    			e.printStackTrace();
    		}
    	}	
    	
    	private static String formatMAC(String str) {
            StringBuilder temMAC = new StringBuilder("");
            for (int i = 1; i <= 12; i++) {
            	temMAC.append(str.charAt(i - 1));
                if (i % 2 == 0) {
                	temMAC.append(":");
                }
            }
            String MAC = temMAC.substring(0, 17);
            return MAC;
        }
    
    	public static void main(String[] args) {
    		String filePath = "wifi600w.dat";
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
    		genPatchMAC("70:00:00:00:00:00",6000000,filePath);
    		System.out.println("finished!");
    	}
    }
    

      

  • 相关阅读:
    AOP动态代理两种方式
    Spring AOP的两种代理方式
    面试中关于Redis的问题看这篇就够了
    关于redis,学会这8点就够了
    什么是MVCC
    JAVA 中BIO,NIO,AIO的理解
    JAVA异常体系结构详解
    Java多线程:由浅入深看synchronized的底层实现原理
    为什么wait,notify和notifyAll要与synchronized一起使用?
    Java并发之AQS详解
  • 原文地址:https://www.cnblogs.com/mosicol/p/11317327.html
Copyright © 2011-2022 走看看