关于文件动手动脑

(1)遍历指定文件夹,输出带有.txt和.java的文件

package JavaApp;

import java.io.IOException;
import java.nio.file.FileSystem;
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 FileFinder extends SimpleFileVisitor<Path> {



    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
            throws IOException {
        Path name=file.getFileName();
        System.out.println("Examining "+name);
        if(matcher.matches(name)){
            foundPaths.add(file);
        }
        return FileVisitResult.CONTINUE;
    }

    private PathMatcher matcher;
    
    public ArrayList<Path> foundPaths=new ArrayList<>();
    
    public FileFinder(String pattern){
        matcher=FileSystems.getDefault().getPathMatcher("glob:"+pattern);    
    }
    
    public static void main(String[] args) {
        Path fileDir=Paths.get("D:\workspace");
        FileFinder finder1=new FileFinder("*.java");
        FileFinder finder2=new FileFinder("*.txt");
        try {
            Files.walkFileTree(fileDir, finder1);
            Files.walkFileTree(fileDir, finder2);
            ArrayList<Path> foundFiles=finder1.foundPaths;
            ArrayList<Path> foundFiles1=finder2.foundPaths;
            if(foundFiles.size()>0){
                for (Path path : foundFiles) {
                System.out.println(path.toRealPath(LinkOption.NOFOLLOW_LINKS));
                }
            }
            else {
                System.out.println("No files were found!");
            }
            if(foundFiles1.size()>0){
                for (Path path : foundFiles1) {
                System.out.println(path.toRealPath(LinkOption.NOFOLLOW_LINKS));
                }
            }
            else {
                System.out.println("No files were found!");
            }
            
        } catch (IOException e) {
        
            e.printStackTrace();
        }

    }

}

运行结果:

(2)找出包容指定字符串的TXT文件

package JavaApp;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
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;

public class filesearch {
     public static void main(String args[]) throws IOException {
            String glob = "glob:**/*.txt";
            String path = "D:\asd";
            match(glob, path);
        }

        public static void match(String glob, String location) throws IOException {

            final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob);

            Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path path,BasicFileAttributes attrs) throws IOException {
                    if (pathMatcher.matches(path)) {
                     BufferedReader reader =Files.newBufferedReader(path);//读取文件内的内容 
                      String line=null;
                      while((line = reader.readLine()) !=null) {
                       if(line.equals("ABC"))//若读取的内容等于“ABC"则输出文件名
                       {
                             System.out.println(path);
                             break;
                       }
                       else {
                           System.out.println("没有对应文件");
                       }
                      }
                    }
                      return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc)
                        throws IOException {
                    return FileVisitResult.CONTINUE;
                }
            });
        }

}

结果:

(3)计算文件总容量

package JavaApp;



import java.io.File;
import java.util.ArrayList;

public class Size {
    static long size = 0;
    private static ArrayList<String> filelist = new ArrayList<String>();

    public static void main(String[] args) {
        Size s = new Size();
        String filePath = "D:";
        s.getFiles(filePath);

    }
    void getFiles(String filePath) {

        File root = new File(filePath);
        File[] files = root.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                getFiles(file.getAbsolutePath());
                filelist.add(file.getAbsolutePath());

            } else {
                size += file.getAbsolutePath().length();
            }
        }
        System.out.println("总容量是" + size);

    }

}

结果:

原文地址:https://www.cnblogs.com/liyuchao/p/9980000.html