`
NumbCoder
  • 浏览: 24167 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Java取得本的IP,MAC和删除指定目录下的文件

    博客分类:
  • Java
阅读更多
/**
     * 取得本地所有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);
        }
    }
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics