Tapping in to ColdFusion's underlying Java capabilities
One of the things I like about ColdFusion is how easy it is to tap into it's underlying Java capabilities. Now I'll be the first to admit that I know virtually nothing about Java, but I have found a few places where tying in to it via CF can be useful. One of the best uses I've found for it (so far) has been working with directories and files.
I really don't have any proof or statistics to say that using a Java object is more efficient than using CF's native tags/functions, but I will say that I have run into issues where a CF tag/function was having problems and using a Java object solved that problem. And, most of these issues have been when working with directories and files. So much so that I now always try to use CF's underlying java capabilities in place of cfdirectory and cffile whenever possible.
Here are a few examples of how to use Java in place of cfdirectory and cffile:
Using Java to create directories instead of cfdirectory:
Let's say we want to create a new directory named "myNewDirectory" in your wwwroot, and we want to create an "images" directory inside of it. With cfdirectory, we would have to make two calls, one to create "myNewDirectory", and another to create "images". But with a Java object, we can create all of the directories at once.
<cfset variables.myDir = "C:\Inetpub\wwwroot\myNewDirectory\images" />
<!--- Create Java object --->
<cfset variables.objFile = CreateObject("java", "java.io.File") />
<!--- Give the Java object our new directory path --->
<cfset variables.fileInstance = variables.objFile.init(variables.myDir) />
<!--- This will create the destination directory as well as any directories missing from its path --->
<cfset variables.fileInstance.mkdirs() />
Pretty simple, huh? Now, I know what you're saying, "you took four lines of code to do what I could have done with two cfdirectory tags". Ok, want to do it with just one line of code?
Using Java to delete a file instead of cffile:
Let's say we want to delete an image from our previously created "images" directory. This is just as simple as creating the directories was in our first example.
<cfset variables.myImg = "C:\Inetpub\wwwroot\myNewDirectory\images\myimage.jpg" />
<!--- Create Java object --->
<cfset variables.objFile = CreateObject("java", "java.io.File") />
<!--- Give the Java object the path to our image. --->
<cfset VARIABLES.fileInstance = VARIABLES.objFile.init(variables.myImg) />
<!--- Delete it. --->
<cfset VARIABLES.fileInstance.Delete() />
Again, if you want to do it with just one line of code:
Using Java to list all files in a directory:
And finally, now that we have our directories created, and have gotten rid of that pesky "myimage.jpg" (not sure how it ever got there in the first place), let's use a Java object to list all of the files in our "images" directory. (Ok, I know there aren't any files in our "images" directory, but we'll pretend someone put some in there when we weren't looking.)
<cfset variables.myDir = "C:\Inetpub\wwwroot\myNewDirectory\images" />
<!--- Create Java object --->
<cfset variables.objFile = CreateObject("java", "java.io.File") />
<!--- Get a list of all directories and files in "images"; listFiles() actually returns an array. --->
<cfset variables.DirList = variables.objFile.init(variables.myDir).listFiles() />
<!--- Loop through the array --->
<cfoutput>
<cfloop from="1" to="#ArrayLen(variables.DirList)#" index="variables.i">
<!--- Display File Path --->
#variables.DirList[variables.i]#<br />
</cfloop>
</cfoutput>
So, code wise there may not be a lot of difference between using Java objects and the native CF tags/functions. In some cases there may actually be a little more code involved in using Java, but in my experience there is definitely a performance increase in using a Java object. As I said before, I don't have any benchmarks or statistics to back that up (although others do), I just know that I've run into numerous cases before where using these Java objects would work great when CF tags/functions would crash and burn.


This must be a new CF 8 addition, because I know for a fact that I wasn't able to do this in previous versions of CF.