Platform Independence with Java
One of the great things about ColdFusion is that it works the same on any supported platform. Still, some things are always different. Carriage returns and directory separators are different based on your OS. Fortunately, you can use Java to bridge the gap.
Directory Path Separators
Windows uses "\" to separate directories but other operating systems use "/". Java, however, will tell you which one is used by your system.
<cfset fileObj = createObject("java", "java.io.File")>
<cfset dirdelim = fileObj.separator>
Now the "dirdelim" variable will hold your directory delimeter ("\" for windows and "/"for other systems).
Carriage Returns
Carriage returns (end of line characters) also vary based on the operating system (Mac uses character #13, Linux uses #10, Windows uses #13 followed by #10).
Until recently I handled this like so:
<cfset cr = "
">
This has always worked, but has always felt a bit ugly. It also sometimes threw off the color-coding of my editor.
After I uploaded XmlHumanReadable, I got an email message from Fabio Serra suggesting a better technique.
<cfset cr = CreateObject("java","java.lang.System").getProperty("line.separator")>
Much like the directory path separators, this returns the carriage return character for your operating system.
Summary
If you take advantage of these Java commands, your code can be easily ported between different platforms without worry.
I am sure there are other simple Java commands that can make our lives easier. If you know any, I would love to hear about them.
There are no comments for this entry.
[Add Comment]