zoukankan      html  css  js  c++  java
  • 动手动脑——流与文件

    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();
            }
        }
    }

  • 相关阅读:
    单节点Redis使用 Python pipline大批量插入数据
    Redis进阶实践之十六 Redis大批量增加数据
    Redis进阶实践之十四 Redis-cli命令行工具使用详解
    Redis进阶实践之十三 Redis的Redis-trib.rb脚本文件使用详解
    (error) MOVED 5798 172.17.0.3:6379
    Redis进阶实践之十二 Redis的Cluster集群动态扩容
    [ERR] Node is not empty. Either the node already knows other nodes (check with C
    【Redis】编译错误zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
    Redis进阶实践之十一 Redis的Cluster集群搭建
    linux 安装软件各种错误集锦及解决方法
  • 原文地址:https://www.cnblogs.com/dream0-0/p/9984782.html
Copyright © 2011-2022 走看看