Luis F. Majano
Java Coldfusion • Computer Engineer
Coldfusion-Java Snippet: Get your Operating System's File Separator.
Every wonder a nice, quick and easy way of getting your Operating System's file separator character??
Well, I have several times. Thanks to our collaborator Ariel Gonzalez, and his java expertise. We take a look at the java.lang.System class and its getProperty() Method.
There are several useful properties that you can get from this class, some of them are listed below:
| Key | Meaning |
|---|---|
| "file.separator" | File separator (for example, "/") |
| "java.class.path" | Java classpath |
| "java.class.version" | Java class version number |
| "java.home" | Java installation directory |
| "java.vendor" | Java vendor-specific string |
| "java.vendor.url" | Java vendor URL |
| "java.version" | Java version number |
| "line.separator" | Line separator |
| "os.arch" | Operating system architecture |
| "os.name" | Operating system name |
| "os.version" | Operating system version |
| "path.separator" | Path separator (for example, ":") |
| "user.dir" | User's current working directory |
| "user.home" | User home directory |
| "user.name" | User account name |
The most useful properties that I have found in Coldfusion Development are:
1) file.separator 2) java.class.version 3) java.version 4) line.separator 5) os.version 6) os.name 7) path.separator
So how do we use this java class in our coldfusion code. Well, pretty simple, just two lines of code.
<cfscript>
//Get a reference to the System class
objSystem = createObject("java", "java.lang.System");
//Get the property desired, in this case, the file separator
fileSeparator = objSystem.getProperty("file.separator");
</cfscript>
//Get a reference to the System class
objSystem = createObject("java", "java.lang.System");
//Get the property desired, in this case, the file separator
fileSeparator = objSystem.getProperty("file.separator");
</cfscript>
and that is it!!! Pretty simple.
[Add Comment]
Is there any other way to do it that does not involve createObject?
You can do a test for windows as the OS, then just do a "\" or "/" for non-windows
[Add Comment]
ColdBox 2.6.1 : FAITH

