Tuesday 21 May 2013

JExcel vs Apache POI : Reading and Writing Excel Files

JExcel is a relatively simple and robust Java based library for manipulating Excel (.xls) files. While it has relatively less functionality than its bigger and more popular brother, Apache POI, it offers a simple API for Excel manipulation.Some features that made me choose JExcel were its relatively smaller memory foot print, simple API and upto date documentation.
On the other hand, some drawbacks that made me wary of choosing the tool were its lack of support for .xlsx files and the fact that the last release was in October 2009, more than 3 years ago. This discussion has listed a significant number of differences between JExcel and POI and while it may appear that Apache POI would be the obvious winner, it all boils down to your requirements. After all you can travel from point A to B in a car or do the same trip in a truck. In situations where the requirements are not very clear, are extensive  or might require support for .xlsx files in the future, Apache POI might be a better choice but if your requirements are clear, you need a fast and efficient Excel generating tool with minimum fuss, choose JExcel.
  
  
   net.sourceforge.jexcelapi
   jxl
   2.6.12
  

  

Thursday 9 May 2013

Java 7 : I/O using the NIO package

The NIO package in Java 7 is an important and much needed update to the Java I/O mechanism. With regards to I/O, the java.nio.file and the java.nio.file.attribute packages are key. An important change to the previous mechanism is the introduction of the Path object which is analogous to the File object. Other changes are documented here.

A simple example that reads in a list of entries from a text file and appends a special character at the end of every line is given below:

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileReaderAndSpecialCharacterInserter {
             
     private static String SPECIAL_CHARACTER = ",";

     /**
       * @param args
      */
     public static void main(String[] args) {
     FileReaderAndSpecialCharacterInserter fileReader = new FileReaderAndSpecialCharacterInserter();
                                fileReader.readFile();

      }
                private void readFile()
                {
                                StringBuffer outputString = new StringBuffer();
                             
                                Path path = Paths.get("c:/test.txt");
                                Charset charset = Charset.forName("US-ASCII");
                                try (BufferedReader reader = Files.newBufferedReader(path, charset))
                                {
                                    String line = null;
                                    while ((line = reader.readLine()) != null)
                                    {
                                        System.out.println(line);
                                        outputString.append(line);
                                        outputString.append(SPECIAL_CHARACTER);
                                    }
                                }
                                catch (IOException x)
                                {
                                    System.err.format("IOException: %s%n", x);
                                }
                             
                                System.out.print("The final output string is : -> " + outputString.toString());
                }
}

---------------
As evident in the example above, the Path object sets up a channel to the file to be read and essays the role played by the File object in the I/O mechanism available upto Java 6. The advantages of this new File I/O mechanism are mainly scalability and error handling  and are documented in detail here.