关闭

JAVA得到网卡物理地址

标签: javaimportnullclasswindowsstring
283人阅读 评论(0) 收藏 举报
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());
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:58875次
    • 积分:854
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:7篇
    • 译文:0篇
    • 评论:16条
    最新评论
    java验证码