我试图读取目录中所有子目录中的所有文件。我已经写了逻辑,但是我做错了一点,因为它在每个文件中读取了两次。
为了测试实现,我创建了一个目录,其中包含三个子目录,每个子目录中都有10个文档。总共应该有30份文件。
这是我正确阅读文档的测试代码:
String basePath = "src/test/resources/20NG";
Driver driver = new Driver();
List<Document> documents = driver.readInCorpus(basePath);
assertEquals(3 * 10, documents.size());
其中Driver#readInCorpus
具有以下代码:
public List<Document> readInCorpus(String directory)
{
try (Stream<Path> paths = Files.walk(Paths.get(directory)))
{
return paths
.filter(Files::isDirectory)
.map(this::readAllDocumentsInDirectory)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}
catch (IOException e)
{
e.printStackTrace();
}
return Collections.emptyList();
}
private List<Document> readAllDocumentsInDirectory(Path path)
{
try (Stream<Path> paths = Files.walk(path))
{
return paths
.filter(Files::isRegularFile)
.map(this::readInDocumentFromFile)
.collect(Collectors.toList());
}
catch (IOException e)
{
e.printStackTrace();
}
return Collections.emptyList();
}
private Document readInDocumentFromFile(Path path)
{
String fileName = path.getFileName().toString();
String outputClass = path.getParent().getFileName().toString();
List<String> words = EmailProcessor.readEmail(path);
return new Document(fileName, outputClass, words);
}
当我运行测试用例时,我看到assertEquals
失败了,因为检索了60个文档,而不是30个,这是不正确的。当我调试时,所有文档都一次插入到列表中,然后以完全相同的顺序再次插入。
我在代码中的哪里读两次文档?
问题出在Files.walk(path)
方法上。您使用不正确。因此,它像树一样遍历文件系统。例如,您有3个文件夹-/parent
和2个子文件夹/parent/first
,/parent/second
。Files.walk("/parent")
将为您Paths
提供每个文件夹的树-父级和2个子级,实际上这是在您的readInCorpus
方法中发生的。
然后,对于每种Path
方法readAllDocumentsInDirectory
,您都在调用第二种方法和同一故事,它遍历文件夹就像一棵树。
对于readAllDocumentsInDirectory
与/parent
路径,它将从两个孩子的文件夹中返回文档/parent/first
和/parent/second
,然后你有2个以上的电话readAllDocumentsInDirectory
了/parent/first
,/parent/second
来自这两个文件夹是文件的回报。
这就是为什么您的文档增加一倍的原因。要解决此问题,您仅应readAllDocumentsInDirectory
使用带Paths.get(basePath)
参数的readInCorpus
方法和remove方法来调用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句