• 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());
        }
    }
  • 相关阅读:
    web服务器iis安全设置
    Log explorer for sql server
    C#将image中的显示的图片转换成二进制
    .Net平台开发的技术规范与实践精华总结
    .net面试题整理
    Javascript 刷新框架及页面的方法总集
    WEB程序代码优化入手的几方面
    用js控制网页播放器
    SQL Server2000中死锁经验总结
    专家谈CSS设计
  • 原文地址:https://www.cnblogs.com/dmir/p/4915813.html
Copyright © 2020-2023  润新知