Wednesday, 22 February 2012

Preserving special characters - Java I/O

I was assigned a bug where a standalone Java programme is writing some content to XML files. The content has few special characters (eg: vulgar fraction one fourth) and during write process the special characters are lost. The end XML file has question marks ('?') in the place of special characters. I suspected that this problem might be related to encoding. After reading a bit more about Java I/O and encoding I found a solution.

1. First I found out the default encoding used by JRE while writing to XML files by using the following code:

OutputStreamWriter out = new OutputStreamWriter(new ByteArrayOutputStream());
System.out.println(out.getEncoding());

The output in my case is 'ASCII', then I established the fact that the encoding is ASCII and doesn't support unicode characters.

2. Here is my old code which uses default encoding.

BufferedWriter out = null;
out = new BufferedWriter(new FileWriter(filename, true));

3. Here is important part where we can specify the encoding we need, in my case its UTF-8. The following code specifies the required encoding when writing files.

Writer out = null;
FileOutputStream fos = new FileOutputStream(filename, true);
out = new OutputStreamWriter(fos, "UTF8");

out.write(buffer.toString());

out.close();

Bingo! After the changes the special characters are properly displayed in the xml.

Wednesday, 1 February 2012

IBM 833 Cleared - Object Oriented Analysis

I wanted to complete IBM Certified Solution Designer (OOAD/UML) certification ever since I came to know about it. The certification contents are very useful and can be applicable to any programming language. The main focus is on Analysis and Design in RUP way. I always believe that solving a problem is a two step process. First we need to fully understand the problem beyond doubt. Second we need to design a solution which shall attack the root of the problem and implement/fix it for good. This certification achieves that or at least gives guidance on that.

I did coding for almost 8 years in Java, and I thought it's the time to move to upper step by focusing on designing. How to shine in software design? By learning to design. As I am lazy and do not want to get my hands dirty without any aim or goal, I am doing this certification to improve my design skills. I also learned very good points on Architecture and it's implications, impact on design etc. There is simply too much valuable stuff which I do not know!

So, I cleared IBM 833 (part 1 analysis) with 86%. I am preparing for IBM 834 (part 2 design). The certification objectives also focuses on RUP and Iterative development which is useful for developers who are potential Project Leaders.