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 :)
@Testpublic 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 structurec:\tempc:\temp\destc:\temp\sourcec:\temp\source\dir1c:\temp\source\dir1\dir3c:\temp\source\dir1\dir3\New Text Document (2).txtc:\temp\source\dir1\dir3\New Text Document.txtc:\temp\source\dir1\New Text Document.txtc:\temp\source\dir2c:\temp\source\dir2\New folderc:\temp\source\dir2\New folder\New folderc:\temp\source\dir2\New folder\New folder\blob.txtc:\temp\source\dir2\New folder\New folder\New folderc:\temp\source\dir2\New folder\New folder\New folder\New Text Document.txtc:\temp\source\dir2\New Text Document.txtc:\temp\source\New Text Document.txtThe 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 blahHellosome moretesttextthen another Hello So the output wasc:\tempc:\temp\destc:\temp\sourcec:\temp\source\dir1c:\temp\source\dir1\dir3c:\temp\source\dir1\dir3\New Text Document (2).txtc:\temp\source\dir1\dir3\New Text Document.txtc:\temp\source\dir1\New Text Document.txtc:\temp\source\dir2c:\temp\source\dir2\New folderc:\temp\source\dir2\New folder\New folderc:\temp\source\dir2\New folder\New folder\blob.txtc:\temp\source\dir2\New folder\New folder\New folderc:\temp\source\dir2\New folder\New folder\New folder\New Text Document.txtc:\temp\source\dir2\New Text Document.txtc:\temp\source\New Text Document.txtblah 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