您的位置 首页 模拟

JAVA得到网卡物理地址(Windows和Linux)

在我们在写程序的过程中,有些时候需要知道一些电脑的硬件信息,比如我们写一些需要注册的程序的时候,就需要得到某个电脑特定的信息,一般来说,网卡

在咱们在写程序的过程中,有些时分需求知道一些电脑的硬件信息,比方咱们写一些需求注册的程序的时分,就需求得到某个电脑特定的信息,一般来说,网卡的物理地址是不会重复的,咱们正好能够用它来做为咱们辨认一台电脑的标志.那怎么得到网卡的物理地址呢?咱们能够借助于ProcessBuilder这个类,这个类是JDK1.5新加的,曾经也能够用Runtime.exce这个类.在此咱们将演示一下怎么在Windows和Linux环境下得到网卡的物理地址.

/*

* Test.java

*

* Created on 2007-9-27, 9:11:15

*

* To change this template, choose Tools   Templates

* and open the template in the editor.

*/

package test2;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Properties;

import java.util.logging.Level;

import java.util.logging.Logger;

/**

*

* @author hadeslee

*/

public class Test {

public static String getMACAddress() {

String address = ;

String os = System.getProperty(os.name);

System.out.println(os);

if (os != null) {

if (os.startsWith(Windows)) {

try {

ProcessBuilder pb = new ProcessBuilder(ipconfig, /all);

Process p = pb.start();

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;

while ((line = br.readLine()) != null) {

if (line.indexOf(Physical Address) != -1) {

int index = line.indexOf(:);

address = line.substring(index + 1);

break;

}

}

br.close();

return address.trim();

} catch (IOException e) {

}

}else if(os.startsWith(Linux)){

try {

ProcessBuilder pb = new ProcessBuilder(ifconfig);

Process p = pb.start();

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;

while((line=br.readLine())!=null){

int index=line.indexOf(硬件地址);

if(index!=-1){

address=line.substring(index+4);

break;

}

}

br.close();

return address.trim();

} catch (IOException ex) {

Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

return address;

}

public static void main(String[] args) {

System.out.println( + Test.getMACAddress());

}

}

咱们能够看一下1.5新增的ProcessBuilder这个类,这个类比起曾经用Runtime.exec来说,要强大一些,它能够指定一个环境 变量,并指定程序运行时的目录空间,而且也能够得到程序运行时的环境变量.由于ipconfig这个指令已经是system32里面的,所以不需求咱们别的再设环境变量或许指定程序的运行时目录空间.咱们直接用就能够了,然后得到进程的输出流,就能够分分出咱们所需求的东西了.是不是挺简略的呢

此程序能够得到windows下和Linux下的网卡地址,不过LINUX要是中文版的,英文版的也相同,只不过把字替换一下就能够了。这样咱们的程序就有了两个渠道的完成。

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/zhishi/moni/321691.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部