Loading

IO流的运用

public class MainTest {
    public static void main(String[] args) throws IOException {
        String source = "C:\\Users\\邓佳\\Desktop\\1\\8970ac3b689863cac0a0e57257ae8ee1.png";
        String target = "C:\\Users\\邓佳\\Desktop\\2\\8970ac3b689863cac0a0e57257ae8ee1.png";
//        fileCopy(source, target);
//        fileCopyNIO(source, target);
//        System.out.println(countWordInFile("C:\\Users\\26632\\Desktop\\新建文本文档 (2).txt", "ROOT"));
//        showFile("C:\\Users\\26632\\Desktop\\file");
//        showDirectoryDeep(new File("C:\\Users\\26632\\Desktop\\file"));
        showDirectoryDeepNIO("C:\\Users\\26632\\Desktop\\file");
    }


    private MainTest() {
//        throw new AssertException();
    }

    /**
     * 文件拷贝
     *
     * @param source 源文件
     * @param target 目标文件
     * @throws IOException e
     */
    public static void fileCopy(String source, String target) throws IOException {
        try (InputStream in = new FileInputStream(source)) {
            try (OutputStream out = new FileOutputStream(target)) {
                byte[] bytes = new byte[4096];
                int readBytesCount;
                while ((readBytesCount = in.read(bytes)) != -1) {
                    out.write(bytes, 0, readBytesCount);
                }
            }
        }
    }

    /**
     * 文件拷贝
     *
     * @param source 源文件
     * @param target 目标文件
     * @throws IOException e
     */
    public static void fileCopyNIO(String source, String target) throws IOException {
        try (FileInputStream in = new FileInputStream(source)) {
            try (FileOutputStream out = new FileOutputStream(target)) {
                FileChannel inChannel = in.getChannel();
                FileChannel outChannel = out.getChannel();
                ByteBuffer buffer = ByteBuffer.allocate(4096);
                while (inChannel.read(buffer) != -1) {
                    buffer.flip();
                    outChannel.write(buffer);
                    buffer.clear();
                }
            }
        }
    }

    /**
     * 统计给定文件中给定字符串的出现次数
     *
     * @param source 文件名
     * @param target 字符串
     * @return 字符串在文件中出现的次数
     * @throws IOException e
     */
    public static int countWordInFile(String fileName, String word) throws IOException {
        int count = 0;
        try (FileReader fr = new FileReader(fileName)) {
            try (BufferedReader br = new BufferedReader(fr)) {
                String line = null;
                while ((line = br.readLine()) != null) {
                    int index = -1;
                    while (line.length() >= word.length() && (index = line.indexOf(word)) >= 0) {
                        count++;
                        line = line.substring(index + word.length());
                    }
                }
            }
        }
        return count;
    }

    /**
     * 展示一个目录下的文件
     *
     * @param dir 目录
     */
    public static void showDirectory(String dir) {
        File file = new File(dir);
        for (File f : file.listFiles()) {
            if (f.isFile()) {
                System.out.println(f.getName());
            }
        }
    }


    /**
     * 展示一个目录下的文件(向下展开)
     *
     * @param dir 目录
     */
    public static void showDirectoryDeep(File dir) {
        walkDirectory(dir, 0);
    }

    private static void walkDirectory(File f, int level) {
        if (f.isDirectory()) {
            for (File temp : f.listFiles()) {
                walkDirectory(temp, level + 1);
            }
        } else {
            for (int i = 0; i < level - 1; i++) {
                System.out.print("\t");
            }
            System.out.println(f.getName());
        }
    }


    /**
     * 展示一个目录下的文件(向下展开)(使用NIO)
     *
     * @param file 文件名
     * @throws IOException e
     */
    public static void showDirectoryDeepNIO(String file) throws IOException {
        Path path = Paths.get(file);
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println(file.getFileName().toString());
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
posted @ 2023-03-21 17:21  溫柔の風  阅读(10)  评论(0编辑  收藏  举报