如何使用Java从文件夹中的所有文件中一一读取数据?

如何使用Java从文件夹中的所有文件(一个一个)中读取数据?


此代码仅返回文件名,我正在寻找更深入的搜索:


File folder = new File("/home/user_name/Downloads/textfiles/");

File[] listOfFiles = folder.listFiles();


for (File file : listOfFiles) {

    if (file.isFile()) {

        System.out.println(file.getName());

    }

}


幕布斯7119047
浏览 227回答 3
3回答

饮歌长啸

你可以在没有任何外部库的情况下,只使用 java.nio。程序如下:代码采用路径 asString并从中创建一个路径(在此示例中java.nio.Path使用常量String路径完成)。然后它准备一个Map<String, List<Strings>将文件名及其内容存储为String每行一个的。在此之后,它会读取使用的目录内容DirectoryStream<Path>从java.nio中进入的文件路径,并同时存储列表,内容阅读Files.readAllLines(Path path)的List<String>,并在绝对文件路径Map。最后,它只是打印出读取的所有内容,由一行长破折号分隔...试试吧,也许对你的搜索有帮助。import java.io.IOException;import java.nio.file.DirectoryStream;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.TreeMap;public class FileContentReader {&nbsp; &nbsp; private static final String FOLDER_PATH = "Y:\\our\\destination\\folder\\";&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Path folderPath = Paths.get(FOLDER_PATH);&nbsp; &nbsp; &nbsp; &nbsp; // prepare a data structure for a file's name and content&nbsp; &nbsp; &nbsp; &nbsp; Map<String, List<String>> linesOfFiles = new TreeMap<String, List<String>>();&nbsp; &nbsp; &nbsp; &nbsp; // retrieve a list of the files in the folder&nbsp; &nbsp; &nbsp; &nbsp; List<String> fileNames = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(folderPath)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Path path : directoryStream) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileNames.add(path.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println("Error reading files");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // go through the list of files&nbsp; &nbsp; &nbsp; &nbsp; for (String file : fileNames) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // put the file's name and its content into the data structure&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> lines = Files.readAllLines(folderPath.resolve(file));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; linesOfFiles.put(file, lines);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // finally, print everything&nbsp; &nbsp; &nbsp; &nbsp; linesOfFiles.forEach((String fileName, List<String> lines) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Content of " + fileName + " is:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lines.forEach((String line) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("————————————————————————————————");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}

侃侃无极

标准的 Java 8 方法是简单地迭代Path给定目录中的所有s:Path inputDir = Paths.get("path/to/dir");if (Files.isDirectory(inputDir)) {&nbsp; &nbsp; List<Path> filePaths = Files.list(inputDir).collect(Collectors.toList());&nbsp; &nbsp; for (Path filePath : filePaths) {&nbsp; &nbsp; &nbsp; &nbsp; // Load file contents...&nbsp; &nbsp; }}您放入循环中的内容取决于您的需要。您可以Reader根据要使用的内存量和性能要求等,制作一个, 流式输出甚至内存映射它。

撒科打诨

使用 commons-io&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>commons-io</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>commons-io</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>2.6</version>&nbsp; &nbsp; </dependency>public static void main(String[] args) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; File files = new File("\\home\\folder");&nbsp; &nbsp; &nbsp; &nbsp; for (File file : files.listFiles()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> lines = FileUtils.readLines(file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("******" + file.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String line : lines) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(file.getName() + "******");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java