Although you can think that Comma-Separated Values (CSV) files are simple files where each value is separated by commas, it is far from reality. The most important part of CSV files are delimiters, and one can think that the delimiter, as type name suggests, is comma. But this assumption is not always true and CSV is often applied to files using other delimiters. Typical example is found in countries where comma (,) is used as decimal separator, because you would have no way to discern between decimal number or separator, semicolon (;) is used.
If your application is distributed across different countries and your application should support Comma-Separated Values files, then you should take care about this important fact.
Let's see how can we resolve this problem so we can read CSV files depending on country.
For reading CSV files we are going to use openCSV library. This API contains one class for reading CSV files (CsvReader) and another for writing (CsvWriter). By default it uses comma as column delimiter, but you can also inform which delimiter character must be used.
So the problem is how to know which delimiter we should use. And for resolving this problem we can use DecimalFormatSymbols class. This class represents the set of symbols needed to format numbers.
DecimalFormatSymbols has a method called getDecimalSeparator() that as his name suggests returns a decimal symbol, comma (,) in case of countries like Germany, and dot (.) in case US for example. So with a simple ternary you could know which delimiter is used in CSV files depending on locale.
So final class will look like:
And previous factory can be instantiated with Spring and injecting delimiter, using Spring Expression module.
See that thanks of Spring Expression delimiter is injected directly into class.
I think it is not difficult to implement an application that can read Localized Comma-Separated Values files, and can avoid having problems when application is distributed around the world.
Full Code: https://github.com/downloads/maggandalf/Blog-Examples/csv.zip
Music: http://www.youtube.com/watch?v=A3Yl3Fsj7tw&feature=related
0 comentarios:
Publicar un comentario