java 生成mac地址

main方法实现生成mac地址。
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;

public class Main {
	public static void main(String[] args) {
		// 生成文件名
		String filePath = "d://macaddr.txt";
		File file = new File(filePath);

		if (file.exists()) {
			file.delete();
		}

		// 设定mac地址起始地址, 以及本次生成的数量
		printMac(filePath, "00:70:A4:00:00:00", 99999);
	}

	/**
	 * 生成mac地址核心逻辑
	 * 
	 * @Description:
	 * @author 666
	 * @date Apr 4, 2018
	 * @param filePath
	 * @param start
	 * @param count
	 */
	private static void printMac(String filePath, String start, int count) {
		start = start.replaceAll(":", "");
		try {
			File file = new File(filePath);
			FileWriter writer = new FileWriter(file, true);
			BigInteger num = new BigInteger(start, 16);
			BigInteger addNum = new BigInteger("1");
			String result = "";
			for (int i = 0; i < count; i++) {
				result = num.toString(16).toUpperCase();
				for (int j = 12 - result.length(); j > 0; j--) {
					result = "0" + result;
				}
				writer.write(getMacAdr(result) + "\n");

				num = num.add(addNum);
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 组成mac地址
	 * 
	 * @Description:
	 * @author 666
	 * @date Apr 4, 2018
	 * @param str
	 * @return
	 */
	private static String getMacAdr(String str) {
		StringBuilder result = new StringBuilder("");
		for (int i = 1; i <= 12; i++) {
			result.append(str.charAt(i - 1));
			if (i % 2 == 0) {
				result.append(":");
			}
		}
		return result.substring(0, 17);
	}
}

你可能感兴趣的:(java 生成mac地址)