zoukankan      html  css  js  c++  java
  • 2020.11.2动手动脑➕课后试验性问题

    一、今日学习内容:

    动手动脑:

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

  • 相关阅读:
    PHP基础学习笔记(一)
    安装wampserver之后,浏览器中输入localhost页面显示IIS7解决办法
    HTML5常识总结(一)
    AngularJs中的服务
    AngularJs中的directives(指令part1)
    Happy Number——LeetCode
    Binary Tree Zigzag Level Order Traversal——LeetCode
    Construct Binary Tree from Preorder and Inorder Traversal——LeetCode
    Construct Binary Tree from Inorder and Postorder Traversal——LeetCode
    Convert Sorted Array to Binary Search Tree——LeetCode
  • 原文地址:https://www.cnblogs.com/marr/p/14176317.html
Copyright © 2011-2022 走看看