java--文件过滤器和简单系统交互
一.文件过滤器
/** * @Title: getFileByFilter * @Description: 根据正则rege获取给定路径及其子路径下的文件名(注意递归的深度不要太大) * @param path * @return 返回类型 */ public static void getFileByFilter(String path,String regex){ File file=new File(path); if(!file.exists() || !file.isDirectory()){ return; } //定义文件过滤器 FileFilter fileFilter=new FileFilter() { @Override public boolean accept(File file) { if(file.getName().endsWith(".txt")) { return true; } return false; } }; //输出满足要求的文件 for(File f:file.listFiles(fileFilter)){ System.out.println(f.getName()); } for(File f:file.listFiles()){ if(f.isDirectory()) { getFileByFilter(f.getAbsolutePath(),regex); } } } public static void main(String[] args) { FileUtil.getFileByFilter("C:\\Users\\Administrator.DONGATE\\Desktop\\xx",".txt"); }
二.简单系统交互
package com.lky.util; import java.net.InetAddress; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Test; public class OperatorSystem { private static Log log = LogFactory.getLog(OperatorSystem.class); public static InetAddress getInetAddress() { InetAddress iAddress = null; try { iAddress = InetAddress.getLocalHost(); } catch (Exception e) { log.info("获取本地地址失败!!!!"); } return iAddress; } public static String getHostIp() { String ip = null; try { ip = getInetAddress().getHostAddress(); } catch (Exception e) { log.info("获取ip失败!!!!"); } return ip; } public static String getHostName() { String hostName = null; try { hostName = getInetAddress().getHostName(); } catch (Exception e) { log.error("获取主机名失败!!!"); } return hostName; } public static boolean isWindows() { String os = System.getProperty("os.name"); System.out.println("本机的操作系统为: "+os); if (os.startsWith("Windows")) { return true; } return false; } @Test public void test() { System.out.println("获取本地主机的Ip: " + OperatorSystem.getHostIp()); System.out.println("获取本地主机的名字: " + OperatorSystem.getHostName()); System.out.println(OperatorSystem.isWindows()); } }
作者:奋斗的珞珞
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.