<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > JAVA得到網(wǎng)卡物理地址(Windows和Linux)

JAVA得到網(wǎng)卡物理地址(Windows和Linux)

作者: 時(shí)間:2016-09-12 來(lái)源:網(wǎng)絡(luò ) 收藏

在我們在寫(xiě)程序的過(guò)程中,有些時(shí)候需要知道一些電腦的硬件信息,比如我們寫(xiě)一些需要注冊的程序的時(shí)候,就需要得到某個(gè)電腦特定的信息,一般來(lái)說(shuō),網(wǎng)卡的物理地址是不會(huì )重復的,我們正好可以用它來(lái)做為我們識別一臺電腦的標志.那如何得到網(wǎng)卡的物理地址呢?我們可以借助于ProcessBuilder這個(gè)類(lèi),這個(gè)類(lèi)是JDK1.5新加的,以前也可以用Runtime.exce這個(gè)類(lèi).在此我們將演示一下如何在Windows和Linux環(huán)境下得到網(wǎng)卡的物理地址.

本文引用地址:http://dyxdggzs.com/article/201609/305055.htm

/*

* 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這個(gè)類(lèi),這個(gè)類(lèi)比起以前用Runtime.exec來(lái)說(shuō),要強大一些,它可以指定一個(gè)環(huán)境 變量,并指定程序運行時(shí)的目錄空間,并且也可以得到程序運行時(shí)的環(huán)境變量.因為ipconfig這個(gè)命令已經(jīng)是system32里面的,所以不需要我們另外再設環(huán)境變量或者指定程序的運行時(shí)目錄空間.我們直接用就可以了,然后得到進(jìn)程的輸出流,就可以分析出我們所需要的東西了.是不是挺簡(jiǎn)單的呢

此程序可以得到windows下和Linux下的網(wǎng)卡地址,不過(guò)LINUX要是中文版的,英文版的也一樣,只不過(guò)把字替換一下就可以了。這樣我們的程序就有了兩個(gè)平臺的實(shí)現。



關(guān)鍵詞:

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>