I am writing a CSV exporter in Java that should respect the user\'s custom settings, especially the \"List separator\" to use as a delimiter.
In Windows, one can set
From comments of this answer:
Reading the OS-specific setting is a need I have to meet.
So what if OSs other than Windows don't have such a setting?
I suggest you read it from registry on Windows (as alluded here): Read/write to Windows Registry using Java. On other platforms just use a good default, and perhaps, at least on Unix, also support configuring it via a custom environment variable (which you document well): How can my java code read OS environment variables?.
My gut feeling that OSs universally do not have a (system-wide or user-specific) "List separator" setting may be wrong, of course, but I doubt that.
In addition to providing your own option to the user in your application you could try to guess what the list separator is.
I had a look at some locales in Windows and saw that the list separator is either ";" or ",". I've heard there is another character in some obscure locale, but have not seen it myself. So if you can make your code to handle both ";" and "," as list separators then you will probably cover majority of cases.
Also, it looks like when "," is used as a decimal separator, then "," is never used as a list separator. I guess this is otherwise numbers will be impossible to distinguish in a list: 1,2,3,4 could be 1.2, 3.4 or 1, 2.3 In these cases ";" is used as a list separator. Unfortunately the reverse is not true. Arabic has "." as a decimal symbol and ";" as a list separator.
So I think the rule that can be reasonably safely followed is:
if (decimalSeparator == ',')
then listSeparator = ';'
else if (decimalSeparator == '.')
then listSeparator = new char[] {';', ','}
For windows it's stored in the registry at:
"HKEY_CURRENT_USER\\Control Panel\\International"
so you can use something like this
private void setDelimiterProperties(String delimiter) {
Properties p = new Properties();
String key = "HKEY_CURRENT_USER\\Control Panel\\International\\sList";
p.setProperty(key, delimiter);
}
Without resorting to a platform specific solution I think that the best approach to take is going to be to allow users to specify their preference of list separator within your own application. Either in a preferences panel, a dialog box on export or via an optional command line argument.
Out of curiosity, I searched a bit the topic, and indeed Java seems to have not such notion out of the box.
The Locales Demo gives a fairly complete listing of locale settings and there is no list separator there.
I saw a forum question referring to sun.text.resources package, which is private and deprecated. You won't find much other references to this package, looks like it lives in jre/lib/ext/localedata.jar although my recent copy of this one lists mostly Asian locales.
The above advices are sound, or you might research and use a private list per locale. I would look perhaps at IBM's ICU library (C language I think) which seems to have a fairly big list of locale settings. According to a remark, ICU itself gets its information from a an ISO standard, which should be researched as primary information provider.
I faced the same problem and also found out that the only proper solution is to provide a way to the user to specify the "delimiter of choice".
However, if that is not possible, as it was in my case, the closest I could get to cross-platfrom, cross-locale support is the following:
This will, at least in most cases (the cases I have tested), force Excel to use that delimiter, even if you "guessed wrong" and will provide at least a little bit more compatibility.