Search This Blog

Loading...

Thursday, July 2, 2015

5 Good Books to Learn Java 8 Functional Programming

TL;DR : I have read many books while learning Java 8, but the the best book I found was Java SE 8 for Really Impatient from Cay S. Horstmann. Why I recommend this book? three reasons, first clarity, second size of the book is not overwhelming, and third and most important I was able to connect to this book. I didn't find it boring or feeling sleepy as soon as I started reading, which in my opinion is a litmus test for any book.  Its been more than a year since Java 8 was launched in March 2014 and number of Java developers looking to learn Java 8 features like lambda expression, streams, and new date and time API is increasing exponentially. Since Java 8 is  significantly different from earlier Java version, thanks to the functional programming approach introduced in this release to take advantage of multicore CPU, programmers are finding it hard to grasp new concepts. Questions like which books should I refer to start learning Java 8? or what is the best book to learn Java 8 is increasingly seen in various places on internet. I had started learning Java 8 from last year itself, even before Java SE 8 was released and I have gone through many books in past one year. When many of my reader asked me to suggest a good book to learn Java 8, I thought about writing this blog post and sharing my experience with some of the best Java 8 books. These books are equally useful for beginners and senior and experience Java programmers looking to learn  functional programming in Java 8.

Wednesday, July 1, 2015

How to append text to existing File in Java? Example

In last tutorial, you have learned about how to write data to a file in Java and in this tutorial you will learn how to append text to a file in Java. What is difference between simply writing to a file vs appending data to a file? In case of writing to a file, program can start writing from start but in case of appending text, you start writing from the end of the file. You can append text into an existing file in Java by opening a file using FileWriter class in append mode. You can do this by using special constructor provided by FileWriter class, which accepts a file and a boolean, which if passed as true then open the file in append mode. This means you can write new content at the end of the file. One of the common example of appending text into file is logging but for that you don't need to write your own logger, there are several good logging library available in Java world e.g. Log4j, SLF4j, Logbak and even java.util.logging is good enough. In this tutorial, you will learn how to append data to an existing text file from Java program. As I said previously, if you are starting fresh in Java then I suggest you to better follow a book because they provide comprehensive coverage, which means you can learn lot of things in quick time. You can follow either core Java by Cay S. Horstmann or Java : A Beginners guide by Herbert Schildt, both are very good book and highly recommended for beginners in Java. Good point about a beginner's guide is that it also covers Java 8 while core Java 9th Edition only covers up-to Java 7.

Tuesday, June 30, 2015

How to write to File in Java using BufferedWriter

You can use either OutputStream or Writer class in Java to write data to a file in Java. For example you can use combination of FileWriter and BufferedWriter to write text content into a text file in Java. If you want to write raw bytes consider using FileOutputStream class. Just remember that InputStream is used to read data and OutputStream is used to write data to file or socket. You can write anything to file e.g. String, integer, float values etc. Java provides DataOutputStream to write different data type directly into file e.g. writeInt() to write integer values, writeFloat() to write floating point values into file and writeUTF() to write String into File.  BufferedWriter, like its counterpart BufferedReader allows you to perform buffered IO, which can drastically improve performance while reading large files. Java provides many convenient wrapper classes for reading and writing data into files e.g. you can use PrintWriter to write data line by line into file. It's println() method automatically adds line separator after each line.  Java 7 has also introduced even a brand new API known as new File API, which provides powerful methods to read whole file in just one line. All in all, Java has got really good support to deal with files in Java and we will explore more of them in coming tutorials. By the way, if you are beginner and just started learning Java, I would recommend you to at-least read one Java book to get a complete overview, later you can fine tune your knowledge by reading tutorials. You can refer Java: A Beginner's Guide by Herbert Schildt to start with Java. This book contains very good example and comprehensive theory and most important its up-to-date and covers even Java 8.