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.
Tuesday, June 30, 2015
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.
Friday, June 26, 2015
You can read a text file in Java 6 by using BufferedReader or Scanner class. Both class provides convenient methods to read a text file line by line e.g. Scanner provides nextLine() method and BufferedReader provides readLine() method. If you are reading a binary file, you can use use FileInputStream. By the way, when you are reading text data, you also need to provide character encoding, if you don't then platform's default character encoding is used. In Java IO, streams like InputStream are used to read bytes and Readers like FileReader are used to read character data. BufferedReader is the traditional way to read data because it reads file buffer by buffer instead of character by character, so its more efficient if you are reading large files. BufferedReader is also there from JDK 1 itself, while Scanner was added on Java 5. Scanner has more features than BufferedReader, when it comes to file reading, for example you can specify any delimiter instead of new line, which is not possible with BufferedReader. Java 7 added new File API, which makes it reading/writing from file even more easier. Its's possible to read entire file in one line in Java 7, but given most of the projects are still running on Java 6, its good to know about these two ways to read a text file in Java. For Java beginners, I also suggest to refer a good book like Cay S. Horstmann, Core Java Volume 1 and 2 to learn basics of Java programming.