我用下面的方法从目录和子目录的文件中获得文本。我得到了这个:
Mar 5 10:00:15 gw1-nc-biva transaction0: O365-1033,O365-104
Mar 5 10:00:15 gw1-nc-biva transaction0: Asset id: 3887
Mar 5 10:00:15 gw1-nc-biva transaction0: Captured transactions:
Mar 5 10:00:15 gw1-nc-biva transaction0: Atlas-1,Atlas-10
Mar 5 10:00:15 gw1-nc-biva transaction0: Asset id: 3888
Mar 5 10:00:15 gw1-nc-biva transaction0: Captured transactions:
Mar 5 10:00:15 gw1-nc-biva transaction0: Google-1,Google-136
Mar 5 10:00:15 gw1-nc-biva transaction1: Log time: Mon, 05 Mar 2018 18:00:05 GMT
Mar 5 10:00:15 gw1-nc-biva transaction1: Asset id: 3886
.
.
.我想从这里创建一个对象并写入一个ArrayList。我该怎么做呢?
public static void pathFiles(File folder) throws IOException {
File[] folderEntries = folder.listFiles();
for (File entry : folderEntries) {
if (entry.isDirectory()) {
pathFiles(entry);
continue;
}
Files.lines(Paths.get(entry.getPath())).forEach(System.out::println);
}
}发布于 2018-05-22 22:13:06
你在这里问的问题不是100%清楚的,因为你没有给出任何关于 object或者the arraylist的信息。
然而,假设您想要收集列表中的文件内容,那么您就差不多完成了。只需使用Collector,而不是像这样打印行:
List<String> lines;
try {
lines = Files.lines(Paths.get(entry.getPath())).collect(Collectors.toList());
}
catch (IOException e) {
System.out.println("Could not read the file.");
e.printStackTrace();
}https://stackoverflow.com/questions/50469620
复制相似问题