Monday, May 26, 2014

Playing with Java 8 - Lambdas, Paths and Files

I needed to read a whole bunch of files recently and instead of just grabbing my old that I and probably most developers have and then copy from project to project, I decided to have quick look at how else to do it...
Yes, I know there is Commons IO and Google IO, why would I even bother?  They probably do it better, but I wanted to check out the NIO jdk classes and play with lambdas aswell.. and to be honest, I think this actually ended up being a very neat bit of code.

So I had a specific use case:
I wanted to read all the source files from a whole directory tree, line by line.

What this code does, it uses Files.walk to recursively get all the paths from the starting point, it creates a stream, which I then filter to only files that end with the required extension. For each of those files, I use Files.lines to create a stream of Strings, one per line. I trim that, filter out the empty ones and add them to the return collection.
All very concise thanks to the new constructs.


  1. This is one such interesting and useful article that i have ever read. The way you have structured the content is so realistic and meaningful. Thank you so much for sharing this in here. Keep up this good work and I'm expecting more contents like this from you in future.

    Hadoop Training Chennai | Hadoop Training in Chennai | big data Training in Chennai

  2. There are many companies that offer professional solutions to the striving business people and help them succeed in their online endeavors. Further, it is platform dependent and runs anywhere, anytime once deployed.
    learn web design

  3. have got some important suggestions from it. You post explain everything in detail and it was very interesting to read. Thank you.
    Web designing course in chennai

  4. Thanks, your article helped me to solve my old problem, cause transient variable java was always one of the most complicated theme for understanding for me. But then I started to read your site and situation quickly turned and the situation quickly turned into a better one.

  5. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..

    Embedded system training in Chennai
    Embedded system Course in Chennai

  6. This news is crucial for all of us.

  7. I enjoyed to visiting your blog. You have done a really nice job.


Popular Posts