java獲取本機mac地址

/*獲取本機mac地址*/
String mac = "";    
        String line = "";  
          
        String os = System.getProperty("os.name");    
          
        if (os != null && os.startsWith("Windows"))   
        {     
            try   
            {     
                String command = "cmd.exe /c ipconfig /all";     
                Process p = Runtime.getRuntime().exec(command);     
                  
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));     
                  
                while((line = br.readLine()) != null)  
                {     
                System.out.println(line);
                    if (line.indexOf("Physical Address") > 0||line.indexOf("物理地址") > 0)  
                    {     
                        int index = line.indexOf(":") + 2;  
                          
                        mac = line.substring(index);  
                          
                        break;     
                    }     
                }     
                  
                br.close();     
                  
            } catch (IOException e) {
            e.printStackTrace();
            }     
        }     
發佈了43 篇原創文章 · 獲贊 22 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章