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());
    }
}
posted @ 2015-10-27 22:20  奋斗的珞珞  阅读(235)  评论(0编辑  收藏  举报