---
title: "Java取得本的IP，MAC和删除指定目录下的文件 "
author: "NumbCoder"
date: "2009-12-08T14:24:51Z"
tags: []
categories: ["编程语言"]
slug: "java-ip-mac"
draft: false
lastmod: "2009-12-08T14:24:51Z"
id: 544057
url: "https://numbcoder.iteye.com/blog/544057"
---

```
/**
     * 取得本地所有IP
     * @return String[]
     */
    private static String[] getAllLocalHostIP() {
        String[] ret = null;
        try {
            String hostName = getLocalHostName();
            if (hostName.length() > 0) {
                InetAddress[] addrs = InetAddress.getAllByName(hostName);
               
                if (addrs.length > 0) {
                    ret = new String[addrs.length];
                    for (int i = 0; i < addrs.length; i++) {
                        ret[i] = addrs[i].getHostAddress();
                    }
                }
            }
        } catch (Exception ex) {
            ret = null;
        }
        return ret;
    }
   
    /**
     * 取得本地所有Mac地址
     * @return List
     */
    public static List getAllLocalHostMac() {

        String line = "";
        List macList = new ArrayList();
       
        Process p = null;
       
        BufferedReader bd = null;
       
        try {
            p = Runtime.getRuntime().exec("cmd.exe /c ipconfig /all");

            bd = new BufferedReader(new InputStreamReader(p
                    .getInputStream()));
            while ((line = bd.readLine()) != null) {
                if (line.indexOf("Physical Address. . . . . . . . . :") != -1) {
                    if (line.indexOf(":") != -1) {
                        String physicalAddress = line.substring(line.indexOf(":") + 2);
                        macList.add(physicalAddress);
                    }
                }
            }
            p.waitFor();
           
        } catch (Exception e) {
            log.error(e.getMessage());
            macList = null;
        } finally {
            p.destroy();
            try {
                bd.close();
            } catch (IOException e) {
                log.error("Buffer Close Error： " + e.getMessage());
            }
        }

        return macList;
    }


    /**
     * 删除指定目录和子目录下的所有文件
     * @author Bian Jiang
     * @since 2008.06.03
     * @param filePath
     */
    public static void delAllFile(String filePath) {
        log.debug("开始删除文件：" + filePath);
        try {
            File file = new File(filePath);
            File[] fileList = file.listFiles();
            String dirPath = null;
            if(fileList != null) {
                for(int i = 0 ; i < fileList.length; i++) {
                    if(fileList[i].isFile()) {
                        fileList[i].delete();
                    }
                    if(fileList[i].isDirectory()){ 
                        dirPath = fileList[i].getPath();
                        delAllFile(dirPath);
                    }
                }
                file.delete();
            }
        } catch (Exception ex) {
            log.error("删除文件失败：" + filePath);
        }
    }
```