file.FileOperatorTest.java Source code

Java tutorial

Introduction

Here is the source code for file.FileOperatorTest.java

Source

/**
 * Copyright (c)2015-2016 https://github.com/javahuang/rp
 * <p>
 * Licensed under Apache License,Version 1.0
 */
package file;

import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * User: Huang rp
 * Date: 16/6/2
 * Version: 1.0
 */
public class FileOperatorTest {

    public static void main(String[] args) throws Exception {
        //        List<Path> path1 = testWalkFileWithStream();
        //        List<Path> path2 = testWalkFile();
        //        System.out.println("path1:" + path1.size() + ", path2:" + path2.size());
        //        for(Path p : path1) {
        //            if(!path2.contains(p)){
        //                System.out.println(p.toFile().getAbsolutePath());
        //            }
        //        }
        //  copyFileUsingFileStreams();
        test11();
    }

    public static final String ROOT_DIR = "/Users/huangrupeng/Downloads";

    /**
     *http://stackoverflow.com/questions/16524065/is-filevisitoption-follow-links-the-only-filevisitoption-available
     */
    public static List<Path> testWalkFileWithStream() {
        try {
            System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "20");
            long a = System.currentTimeMillis();
            Files.walk(Paths.get(ROOT_DIR), FileVisitOption.FOLLOW_LINKS).parallel().filter(x -> {
                return x.toFile().isFile();
            }).forEach(x -> {
                // Files.copy(x, Paths.get(""), CopyOption )
            });
            long b = System.currentTimeMillis();
            System.out.println(":" + (b - a));
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static List<Path> testWalkFile() throws Exception {
        List<Path> paths = Lists.newArrayList();
        long a = System.currentTimeMillis();
        Files.walkFileTree(Paths.get(ROOT_DIR), new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                paths.add(file);
                return FileVisitResult.CONTINUE;
            }

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

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
        long b = System.currentTimeMillis();
        System.out.println(":" + (b - a));
        return paths;
    }

    public static final String SOURCE_FILE = "/Users/huangrupeng/Downloads/CentOS-6.5-x86_64-bin-DVD1.iso";
    public static final String DEST_FILE = "/tmp/test.iso";

    public static void copyFileUsingFileStreams() {
        long a = System.currentTimeMillis();
        InputStream input = null;
        OutputStream out = null;
        try {
            input = new FileInputStream(SOURCE_FILE);
            out = new FileOutputStream(DEST_FILE);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                out.write(buf, 0, bytesRead);
            }

        } catch (Exception e) {
        } finally {
            try {
                input.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long b = System.currentTimeMillis();
        System.out.println(":" + (b - a));
    }

    public static void test11() {
        long a = System.currentTimeMillis();
        try {
            new File(DEST_FILE).createNewFile();
            FileUtils.copyFile(new File(SOURCE_FILE), new File(DEST_FILE));
        } catch (IOException e) {
            e.printStackTrace();
        }
        long b = System.currentTimeMillis();
        System.out.println(":" + (b - a));//18460 18342
    }
}