Java取得本的IP,MAC和删除指定目录下的文件
/** * 取得本地所有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; for (int i = 0; i < addrs.length; i++) { ret = addrs.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.isFile()) { fileList.delete(); } if(fileList.isDirectory()){ dirPath = fileList.getPath(); delAllFile(dirPath); } } file.delete(); } } catch (Exception ex) { log.error("删除文件失败:" + filePath); } }
页:
[1]