Java获取物理网卡_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java获取物理网卡

Java获取物理网卡

 2013/7/18 0:14:19  qincidong  程序员俱乐部  我要评论(0)
  • 摘要:importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.util.HashSet;publicclassProcessBuilderShow{publicstaticHashSet<String>getPhysicalAddress(Stringtext)throwsIOException
  • 标签:Java
class="java"> import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;

public class ProcessBuilderShow {
    public static HashSet<String> getPhysicalAddress(String text) throws IOException {
        Process p = null;
        // 物理网卡列表
        HashSet<String> address = new HashSet<String>();

        // 执行ipconfig /all命令
        p = new ProcessBuilder("ipconfig", "/all").start();

        // 读取进程输出值
        InputStream in = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String temp = null;
        while ((temp = br.readLine()) != null) {
            System.out.println(temp);
            int idx = temp.indexOf(text);
            if (idx > 0) {
                address.add(temp.substring(text.length()+4,temp.length()));
            }
        }
        
        return address;
    }
    
    public static void main(String[] args) {
        String[] texts = new String[]{"Physical Address. . . . . . . . . :","物理地址. . . . . . . . . . . . . :"};
        HashSet<String> address;
        try {
            address = ProcessBuilderShow.getPhysicalAddress(texts[0]);
            if (address == null || address.size() == 0) {
                address = ProcessBuilderShow.getPhysicalAddress(texts[1]);                
            }
            System.out.println("物理地址列表:");
            for (String add : address) {
                System.out.println(add);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}

?

发表评论
用户名: 匿名