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.
Wednesday, July 1, 2015
Tuesday, June 30, 2015
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.
Saturday, June 27, 2015
Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException comes when your program is using DOM4j library but necessary JAR is not present. This error can also come when you are indirectly using DOM4j library e.g. when you use Apache POI library to read XLSX file in Java, this library need dom4j.jar in your classpath. Not just this one but there are several other libraries which uses this JAR internally, if you are using any of them but don't have this JAR then your program will compile fine but fail at runtime because JVM will try to load this class but will not be able to find it on classpath. Some curious developers might ask, why it didn't fail during compile time, if JAR was not present there? Well reason for that is that your code might not be using any class file directly from the dom4j.jar file. When you compile your program e.g. ExcelReader.java, then compiler will only look for class file which is directly referenced or required to generate class file, it will not look for transitive dependencies. For example, suppose you need WorkBook class from Apache POI, which internally uses dom4j library, compiler will complain if poi.jar is not there, but will not complain even if dom4j.jar is not present because its compiling your code, its not compiling WorkBook class, its already compiled because class file of this code is already present in poi.jar. But things works differently when your run the program, at that time JVM will look for all the binary code. When it will load WorkBook class it will try to load the referenced class from DOM4j library and at that time if JVM didn't find those classes in CLASSPATH, it will throw java.lang.NoClassDefFoundError: org/dom4j/DocumentException. It can also come as java.lang.ClassNotFoundException: org.dom4j.DocumentException but only if your code tries to load this class explicitly instead of JVM. That's actually the difference between ClassNotFoundException and NoClassDefFoundError in Java. Now million dollar question? How do you solve this error? Just add dom4j-1.6.1.jar file into classpath.