zoukankan      html  css  js  c++  java
  • 基于NIO2的遍历文件夹简单复制

        public Class CopyAndWrite {
    
            public static final String SOURCES = "D:\sources";
            public static final String TARGET = "D:\target";
    
            public static void main (String[]args) throws IOException {
    
                Path startingDir = Paths.get(SOURCES);
        
                Files.walkFileTree(startingDir, new FindJavaVisitor());
            }
    
            private static class FindJavaVisitor extends SimpleFileVisitor<Path> {
    
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    if (!StringUtils.equals(dir.toString(), SOURCES)) {
                        Path targetPath = Paths.get(TARGET + dir.toString().substring(SOURCES.length()));
                        if (!Files.exists(targetPath)) {
                            Files.createDirectory(targetPath);
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }
    
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Path targetPath = Paths.get(TARGET + file.toString().substring(SOURCES.length()));
                    copyFile(targetPath, Files.readAllBytes(file));
    
                    return FileVisitResult.CONTINUE;
                }
            }
    
            private static void copyFile (Path path,byte[] bytes){
                // write file
                try {
                    Files.write(path, bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
  • 相关阅读:
    51NOD 1069 Nim游戏
    51NOD 1066 Bash游戏
    51NOD 1058 N的阶乘的长度
    51NOD 1057 N的阶乘
    51NOD 1027 大数乘法
    RMQ 区间最大值 最小值查询
    Codeforces Round #426 (Div. 2) C. The Meaningless Game
    51NOD 1046 A^B Mod C
    OJ上 编译器 G++和C++的区别
    二分暑假专题 训练记录 2017-7-29
  • 原文地址:https://www.cnblogs.com/Deters/p/11595624.html
Copyright © 2011-2022 走看看