zoukankan      html  css  js  c++  java
  • java 核心编程——文件过滤类(FileFilter和FilenameFilter)

    1.FileFilter

    package test;
    
    import java.io.File;
    import java.io.FileFilter;
    
    public class FileFilterTest implements FileFilter {
        
        
        private String extension;
        
        public FileFilterTest(String extension) {
    
            this.extension = extension;
        }
        /**
         * 
         */
        @Override
        public boolean accept(File pathname) {
                
            if(pathname.isDirectory()){
                return false;
            }
            
            String name = pathname.getName();
            int num = name.lastIndexOf(".");
            if(num == -1){
                return false;
            }
            if(name.length()-1 == num){
                return false;
            } else {
            
                return extension.equals(name.substring(num+1));
            }
        }
        
        
        public static void main(String[] args) {
            
            File path = new File("E:\书籍\java 编程");
            //获得过滤对象
            FileFilterTest ft = new FileFilterTest("pdf");
            
            File[] files = path.listFiles(ft);
            
            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i].getAbsolutePath());
            } 
            
        }
    
    }

    2.FilenameFilter

    package test;
    
    import java.io.File;
    import java.io.FilenameFilter;
    
    public class FileNameFilterTest implements FilenameFilter {
        
        //判断图片是否格式是否是 gif
        private boolean isGif(String fileName){
            if(fileName.toLowerCase().endsWith(".gif")){
                return true;
            } else {
                return false;
            }
            
        }
        //判断图片是否格式是否是 jpg
        private boolean isJpg(String fileName){
            if(fileName.toLowerCase().endsWith(".jpg")){
                return true;
            } else {
                return false;
            }
            
        }
        //判断图片是否格式是否是 png
        private boolean isPng(String fileName){
            if(fileName.toLowerCase().endsWith(".png")){
                return true;
            } else {
                return false;
            }
            
        }
        
        
    
        @Override
        public boolean accept(File dir, String name) {
            return (isGif(name)||isJpg(name)||isPng(name));
        }
        
        public static void main(String[] args) {
            File path = new File("C:\img");
            //获得过滤对象
            FileNameFilterTest ft = new FileNameFilterTest();
            
            File[] f = path.listFiles(ft);
            
            for (int i = 0; i < f.length; i++) {
                File file = f[i];
                
                System.out.println(file.getAbsolutePath());
                
            }
        }
    
    }
  • 相关阅读:
    VLC在web系统中应用(xvlcplugin 即如何把VLC嵌入HTML中)
    mysql in 排序
    EditPlus v3.31 注册码
    UTF8编码判断
    zend framework 获取邮箱内容 编码转换 quoted_printable_decode | base64_decode
    String path = request.getContextPath(....拼装当前网页的相对路径
    【转】input中id和name的区别
    JSON基础知识
    【转】 jdbc.properties
    JSP页面传值乱码过滤
  • 原文地址:https://www.cnblogs.com/wwyx-xi/p/7423599.html
Copyright © 2011-2022 走看看