• 2020.10.6收获(动手动脑六)


    1.使用Files. walkFileTree()找出指定文件夹下所有大于指定大小(比如1M)的文件。

    复制代码
    package filesoperation;
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.FileVisitResult;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.SimpleFileVisitor;
    import java.nio.file.attribute.BasicFileAttributes;
    
    
    public class SearchSmallFiles extends SimpleFileVisitor<Path> {
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            File f = new File(file.toString());
            if(f.length() > 1024 * 1024 *1)
                System.out.println(file.toString() + "大小为:" + (double)f.length()/1024/1024 + "M。");
            return FileVisitResult.CONTINUE;
        }
        public static void main(String[] args) {
            Path fileDirPath=Paths.get("F:\学习\作业\Java");
            SearchSmallFiles visitor=new SearchSmallFiles();
            try {
                Files.walkFileTree(fileDirPath, visitor);
            } catch (IOException e) {
                e.printStackTrace();
            }    
        }
    }
    复制代码

    2.使用Files. walkFileTree()找出指定文件夹下所有扩展名为.txt和.java的文件。

    复制代码
    package filesoperation;
    
    import java.io.IOException;
    import java.nio.file.FileSystems;
    import java.nio.file.FileVisitResult;
    import java.nio.file.Files;
    import java.nio.file.LinkOption;
    import java.nio.file.Path;
    import java.nio.file.PathMatcher;
    import java.nio.file.Paths;
    import java.nio.file.SimpleFileVisitor;
    import java.nio.file.attribute.BasicFileAttributes;
    import java.util.ArrayList;
    
    public class SearchTXTAndJAVA extends SimpleFileVisitor<Path> {
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException {
            Path name=file.getFileName();
            if(matcher.matches(name)){
                foundPaths.add(file);
            }
            return FileVisitResult.CONTINUE;
        }
        private PathMatcher matcher;    
        public ArrayList<Path> foundPaths=new ArrayList<>();
        public SearchTXTAndJAVA(String pattern){
            matcher=FileSystems.getDefault().getPathMatcher("glob:"+pattern);    
        }    
        public static void main(String[] args) {
            Path fileDir=Paths.get("F:\学习\作业\Java");
            SearchTXTAndJAVA finder1 = new SearchTXTAndJAVA("*.txt");
            SearchTXTAndJAVA finder2 = new SearchTXTAndJAVA("*.java");
            try {
                Files.walkFileTree(fileDir, finder1);
                ArrayList<Path> foundFiles1=finder1.foundPaths;
                if(foundFiles1.size()>0){
                    for (Path path : foundFiles1) {
                    System.out.println(path.toRealPath(LinkOption.NOFOLLOW_LINKS));
                    }
                }else {
                    System.out.println("未发现".txt"文件!");
                }    
                Files.walkFileTree(fileDir, finder2);
                ArrayList<Path> foundFiles2=finder2.foundPaths;
                if(foundFiles2.size()>0){
                    for (Path path : foundFiles2) {
                    System.out.println(path.toRealPath(LinkOption.NOFOLLOW_LINKS));
                    }
                }else {
                    System.out.println("未发现".java"文件!");
                }        
            } catch (IOException e) {    
                e.printStackTrace();
            }
        }
    }
    复制代码

    3.使用Files. walkFileTree()找出指定文件夹下所有包容指定字符串的txt文件。

    复制代码
    package filesoperation;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.nio.file.FileSystems;
    import java.nio.file.FileVisitResult;
    import java.nio.file.Files;
    import java.nio.file.LinkOption;
    import java.nio.file.Path;
    import java.nio.file.PathMatcher;
    import java.nio.file.Paths;
    import java.nio.file.SimpleFileVisitor;
    import java.nio.file.attribute.BasicFileAttributes;
    import java.util.ArrayList;
    
    public class SearchTheTXT extends SimpleFileVisitor<Path> {
        private PathMatcher matcher;    
        public ArrayList<Path> foundPaths=new ArrayList<>();
        public SearchTheTXT(String pattern){
            matcher=FileSystems.getDefault().getPathMatcher("glob:"+pattern);    
        }
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)throws IOException {
            Path name = file.getFileName();
            if(matcher.matches(name)){
                BufferedReader br = new BufferedReader(new FileReader(file.toString()));
                String str;
                boolean isTheTXT = false;
                while((str = br.readLine()) != null) {
                    if(str.indexOf("life") != -1) {
                        isTheTXT = true;
                        break;
                    }
                }
                if(isTheTXT)
                    foundPaths.add(file);
                br.close();
            }
            return FileVisitResult.CONTINUE;
        }
        public static void main(String[] args) {
            Path fileDir=Paths.get("F:\学习\作业");
            SearchTheTXT finder = new SearchTheTXT("*.txt");
            try {
                Files.walkFileTree(fileDir, finder);
                ArrayList<Path> foundFiles1=finder.foundPaths;
                if(foundFiles1.size()>0){
                    for (Path path:foundFiles1) {
                        System.out.println(path.toRealPath(LinkOption.NOFOLLOW_LINKS));
                    }
                }else {
                    System.out.println("未发现含有"life"的".txt"文件!");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    复制代码

  • 相关阅读:
    解决Maven下载依赖慢
    Spring Boot系列教程六:日志输出配置log4j2
    Spring Boot系列教程三:使用devtools实现热部署
    Spring Boot系列教程五:使用properties配置文件实现多环境配置
    Spring Boot系列教程四:配置文件详解properties
    Spring Boot系列教程二:创建第一个web工程 hello world
    Spring Boot系列教程一:Eclipse安装spring-tool-suite插件
    Spring Boot系列教程十:Spring boot集成MyBatis
    vim入门一 常用指令
    Linux IO多路复用之epoll网络编程(含源码)
  • 原文地址:https://www.cnblogs.com/ltw222/p/13965638.html
Copyright © 2020-2023  润新知