Ich habe folgende Verzeichnisstrukturen:Wie verwendet man Files.walk() ... um ein Diagramm von Dateien basierend auf Bedingungen zu erhalten?
/path/to/stuff/org/foo/bar/
/path/to/stuff/org/foo/bar/1.2.3/
/path/to/stuff/org/foo/bar/1.2.3/myfile.ext
/path/to/stuff/org/foo/bar/1.2.4/
/path/to/stuff/org/foo/bar/1.2.4/myfile.ext
/path/to/stuff/org/foo/bar/blah/
/path/to/stuff/org/foo/bar/blah/2.1/
/path/to/stuff/org/foo/bar/blah/2.1/myfile.ext
/path/to/stuff/org/foo/bar/blah/2.2/
/path/to/stuff/org/foo/bar/blah/2.2/myfile.ext
Ich mag würde die folgende Ausgabe erhalten:
/path/to/stuff/org/foo/bar/
/path/to/stuff/org/foo/bar/blah/
Ich habe den folgenden Code (siehe unten), was ineffizient ist, wie es aus druckt:
/path/to/stuff/org/foo/bar/
/path/to/stuff/org/foo/bar/
/path/to/stuff/org/foo/bar/blah/
/path/to/stuff/org/foo/bar/blah/
Hier ist die Java-Code:
public class LocatorTest
{
@Test
public void testLocateDirectories()
throws IOException
{
long startTime = System.currentTimeMillis();
Files.walk(Paths.get("/path/to/stuff/"))
.filter(Files::isDirectory)
.forEach(Foo::printIfArtifactVersionDirectory);
long endTime = System.currentTimeMillis();
System.out.println("Executed in " + (endTime - startTime) + " ms.");
}
static class Foo
{
static void printIfArtifactVersionDirectory(Path path)
{
File f = path.toAbsolutePath().toFile();
List<String> filePaths = Arrays.asList(f.list(new MyExtFilenameFilter()));
if (!filePaths.isEmpty())
{
System.out.println(path.getParent());
}
}
}
}
Der Filter:
public class MyExtFilenameFilter
implements FilenameFilter
{
@Override
public boolean accept(File dir, String name)
{
return name.endsWith(".ext");
}
}
'.allMatch()' kompiliert nicht. – Seelenvirtuose
@Seelenvirtuose: Entschuldigung. Behoben. – carlspring