列出目录下文件名称

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class Test {
    public static void main(String[] args) throws Exception {
        Path initPath = Paths.get(DIRECTORY_NAME);
        FileWriter fw = new FileWriter(CACHE_FILE_NAME);
        PrintWriter pw = new PrintWriter(fw);
        Files.walkFileTree(initPath, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                pw.println(path);
                pw.flush();
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页