Wednesday, 26 November 2014

Nice Java8 Streams

Java 8 Stream
I just threw a quick unit test together to see how a grep like tool would work in Java 8 using some nice streams stuff and this code looked so nice I thought I'd frame it an hang it on the web :)

@Test
public void testFindTxtFilesAndGetContentsThatMatchRegEx() throws IOException {
    Path start = Paths.get( "c:/temp" );
    BiPredicate<Path, BasicFileAttributes> bi = new BiPredicate<Path, BasicFileAttributes>() {
        @Override public boolean test(Path t, BasicFileAttributes u) {
            System.out.println( t );
            return t.toString().endsWith( "txt" );
        }
    };     
    System.out.println( Files.find( start, Integer.MAX_VALUE, bi )
            .filter( p -> Files.isRegularFile( p ) )
            .filter( p -> Files.isReadable( p ) )
            .map( this::myFunc )
            .collect( Collectors.joining() ) );
}
 
private String myFunc( final Path t ) {
    try {
        return Files.lines( t )
                .filter( p -> p.matches( ".*Hello.*" ) )
                .collect( Collectors.joining() );
    } catch ( IOException ioe ) {
        return "";
    }
}
 
 
I just really like the concise simplicity of this one :)
 
Sadly it was made on a Winblows machine so it's a dfs rooted at c:\temp that examines files with a .txt extension and reports any lines that match the regex ".*Hello.*"
 
so from this file structure
c:\temp
c:\temp\dest
c:\temp\source
c:\temp\source\dir1
c:\temp\source\dir1\dir3
c:\temp\source\dir1\dir3\New Text Document (2).txt
c:\temp\source\dir1\dir3\New Text Document.txt
c:\temp\source\dir1\New Text Document.txt
c:\temp\source\dir2
c:\temp\source\dir2\New folder
c:\temp\source\dir2\New folder\New folder
c:\temp\source\dir2\New folder\New folder\blob.txt
c:\temp\source\dir2\New folder\New folder\New folder
c:\temp\source\dir2\New folder\New folder\New folder\New Text Document.txt
c:\temp\source\dir2\New Text Document.txt
c:\temp\source\New Text Document.txt

The files c:\temp\source\dir2\New folder\New folder\blob.txt and c:\temp\source\New Text Document.txt have the content

blah blah Hello blah
Hello
some more
test
text
then another Hello
 
 So the output was

c:\temp
c:\temp\dest
c:\temp\source
c:\temp\source\dir1
c:\temp\source\dir1\dir3
c:\temp\source\dir1\dir3\New Text Document (2).txt
c:\temp\source\dir1\dir3\New Text Document.txt
c:\temp\source\dir1\New Text Document.txt
c:\temp\source\dir2
c:\temp\source\dir2\New folder
c:\temp\source\dir2\New folder\New folder
c:\temp\source\dir2\New folder\New folder\blob.txt
c:\temp\source\dir2\New folder\New folder\New folder
c:\temp\source\dir2\New folder\New folder\New folder\New Text Document.txt
c:\temp\source\dir2\New Text Document.txt
c:\temp\source\New Text Document.txt
blah blah Hello blahHellothen another Helloblah blah Hello blahHellothen another Hello
 
So you can see it duplicated the lines containing Hello because they were found in 2 files, it didn't print any lines that didn't contain Hello from any other files in very little code.
 
Just thought I'd share :)

No comments:

Post a Comment