Home  >  Article  >  Java  >  Java how to loop through a folder and get all file names

Java how to loop through a folder and get all file names

PHPz
PHPzOriginal
2024-03-29 13:24:021001browse

Java how to loop through a folder and get all file names

Java is a popular programming language with powerful file processing capabilities. In Java, traversing a folder and getting all file names is a common operation, which can help us quickly locate and process files in a specific directory. This article will introduce how to implement a method of traversing a folder and getting all file names in Java, and provide specific code examples.

1. Use the recursive method to traverse the folder

We can use the recursive method to traverse the folder. The recursive method is a way of calling itself, which can effectively traverse all the files in the folder. files and subfolders. The following is an example of a simple recursive method:

import java.io.File;

public class FileTraversal {
    
    public static void main(String[] args) {
        File folder = new File("路径"); // 需要遍历的文件夹路径
        
        traverseFiles(folder);
    }
    
    public static void traverseFiles(File folder) {
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        traverseFiles(file); // 递归遍历子文件夹
                    } else {
                        System.out.println(file.getName()); // 输出文件名
                    }
                }
            }
        }
    }
}

2. Traverse folders using Java 8’s Stream API

Java 8 introduces the Stream API, which simplifies the processing of collections and arrays. We can combine the Files.walk() method and the Stream API to walk through the folder and get all the file names. Here is an example using the Stream API:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class FileTraversal {
    
    public static void main(String[] args) throws IOException {
        Path folder = Paths.get("路径"); // 需要遍历的文件夹路径
        
        try (Stream<Path> paths = Files.walk(folder)) {
            paths
                .filter(Files::isRegularFile) // 过滤只获取文件
                .forEach(path -> System.out.println(path.getFileName())); // 输出文件名
        }
    }
}

3. Traverse the folder using Java 7’s Files.walkFileTree() method

If you are using Java 7, you can use Files. walkFileTree() method to traverse the folder. This method needs to implement the FileVisitor interface and override the visitFile() method to obtain file information. The following is an example of using the Files.walkFileTree() method:

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

public class FileTraversal {
    
    public static void main(String[] args) throws IOException {
        Path folder = Paths.get("路径"); // 需要遍历的文件夹路径
        
        Files.walkFileTree(folder, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println(file.getFileName()); // 输出文件名
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

Conclusion

Through the above three methods, we can implement the function of traversing the folder and obtaining all file names in Java. This operation can be easily achieved whether using the recursive method, the Stream API or the Files.walkFileTree() method. Depending on actual needs and Java versions, choose different methods for folder traversal. I hope this article can be helpful to you. For more knowledge about Java programming, please follow our blog.

The above is the detailed content of Java how to loop through a folder and get all file names. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn