码迷,mamicode.com
首页 > 编程语言 > 详细

Java批量生成Mac地址到文件

时间:2015-06-08 11:37:29      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

public class Main {

    public static void main(String[] args) {
        // 生成文件名
        String filePath = "mac.txt";
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
        // 设定起始地址, 以及数量
        printMac(filePath, "00:70:A4:00:00:00", 100000);
    }

    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();
            System.out.println("finished");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    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地址到文件

标签:

原文地址:http://blog.csdn.net/brian512/article/details/46410247

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!