zoukankan      html  css  js  c++  java
  • Guava Files

    Guava Files提供了多种直接复制、写入、读取文件的方法,可以便捷的直接操作,使用较为方便,并且具有丰富灵活的API

    private static final String SOURCE_FILE = "D:\project\intellij-git\xc-springboot\src\main\resources\source.txt";
    private static final String TARGET_FILE = "D:\project\intellij-git\xc-springboot\src\main\resources\target.txt";
    
    @Test
    public void testCopyFile() throws IOException {
        //复制文件
        File targetFile = new File(TARGET_FILE);
        File sourceFile = new File(SOURCE_FILE);
        Files.copy(new File(SOURCE_FILE), targetFile);
        assertThat(targetFile.exists(), equalTo(true));
        // 判断hash是否一致
        HashCode sourceHash = Files.asByteSource(sourceFile).hash(Hashing.sha256());
        HashCode targetHash = Files.asByteSource(targetFile).hash(Hashing.sha256());
    
        assertThat(sourceHash.equals(targetHash), equalTo(true));
    }
    
    @Test
    public void testMoveFile() throws IOException {
        //移动文件
        File targetFile = new File(TARGET_FILE);
        try {
            Files.move(new File(SOURCE_FILE), targetFile);
        } catch (IOException e) {
    
        } finally {
            Files.move(targetFile, new File(SOURCE_FILE));
        }
    }
    
    @Test
    public void testToString() throws IOException {
        //读取文件内容
        String string = "this is a source file, will be used to test Guava files";
    
        List<String> strings = Files.readLines(new File(SOURCE_FILE), Charsets.UTF_8);
        String stringlist = Joiner.on("
    ").join(strings);
    
        assertThat(string, equalTo(stringlist));
    }
    
    @Test
    public void testFileLineProcess() throws IOException {
        //自定义读取文件函数
        // 在读取文件每一行数据的时候,可以通过重写方法实现,是否做额外的处理或者是否读取改行数据
        File sourceFile = new File(SOURCE_FILE);
    
        // 通过LinePorcess来对读取的每行数据进行处理
        List<Integer> list = Files.asCharSource(sourceFile, Charsets.UTF_8).readLines(new LineProcessor<List<Integer>>() {
            List<Integer> list = new ArrayList<>();
    
            @Override
            public boolean processLine(String s) throws IOException {
                // 返回是否处理该行
                if (Strings.isNullOrEmpty(s)) {
                    return false;
                }
                list.add(s.length());
                return true;
            }
    
            @Override
            public List<Integer> getResult() {
                return list;
            }
        });
    
    }
    
    @Test
    public void testFileAppend() throws IOException {
        // Append方式写问文件
        File file = new File(SOURCE_FILE);
        // 使用APPEND模式向文件中追加内容
        CharSink charSink = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
        charSink.write("content");
    
        // 通过CharSource读取文件
        CharSource charSource = Files.asCharSource(file, Charsets.UTF_8);
        String read = charSource.read();
    
        assertThat(read, equalTo("content"));
    }
    
    /**
     * 递归遍历指定路径
     * 1.获取所有的路径
     * 2.获取所有的文件
     * <p>
     * 手动实现
     */
    @Test
    public void testRecursiveFile() {
        File file = new File("D:\project\intellij-git\xc-springboot\src\main\resources");
        List<String> pathList = Lists.newArrayList();
        // 递归遍历所有的目录
        findAllFiles(file, pathList);
        // 输出所有的目录路径
        pathList.forEach(System.out::println);
    
        // 递归遍历所有的文件
        List<String> files = Lists.newArrayList();
        findAllFiles(file, files);
        // 输出指定目录下所有的文件
        files.forEach(System.out::println);
    }
    
    private void findAllFiles(File file, List<String> files) {
        if (file.isHidden()) {
            return;
        }
        if (file.isFile()) {
            files.add(file.getPath());
        }
        if (!file.isFile()) {
            File[] list = file.listFiles();
            for (File f : list) {
                findAllFiles(f, files);
            }
        }
    }
    
    @Test
    public void testTreeFIle() {
        //Guava的API递归遍历文件
        File file = new File("D:\project\intellij-git\xc-springboot\src\main\resources");
        // 使用前序遍历方式进行递归遍历
        Iterable<File> files = Files.fileTraverser().depthFirstPreOrder(file);
        files.forEach(System.out::println);
    
        // 使用后序遍历的方式递归遍历
        Iterable<File> files1 = Files.fileTraverser().depthFirstPostOrder(file);
        files1.forEach(System.out::println);
    
        // 使用广度优先遍历的方式递归遍历
        Iterable<File> files2 = Files.fileTraverser().breadthFirst(file);
        files2.forEach(System.out::println);
    }
  • 相关阅读:
    使用element-ui table expand展开行实现手风琴效果
    使用js生成二维码和条形码
    js时间戳转换时间、距当前时间
    使用js在浏览器中禁止右键、审查元素、复制功能
    VS Code编辑器插件整理及配置设定
    在vue项目中使用canvas-nest.js,报parameter 1 is not of type 'Element'
    JS中的函数
    Babel 7 初探
    package-lock.json 文件
    Js 中的数组
  • 原文地址:https://www.cnblogs.com/ooo0/p/15011488.html
Copyright © 2011-2022 走看看