{"id":441,"date":"2003-06-28T22:54:48","date_gmt":"2003-06-29T02:54:48","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=441"},"modified":"2003-06-28T22:54:48","modified_gmt":"2003-06-29T02:54:48","slug":"calling-java-constructors-with-coldfusion","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/06\/28\/calling-java-constructors-with-coldfusion\/","title":{"rendered":"Calling Java Constructors with ColdFusion"},"content":{"rendered":"<p>I <a href=\"http:\/\/www.rewindlife.com\/archives\/000023.cfm\">mentioned<\/a> yesterday that Sam posted some great code that lets you upload files to ColdFusion without using CFFILE.  His code can be shortened considerably with the use of the <a href=\"http:\/\/livedocs.macromedia.com\/cfmxdocs\/CFML_Reference\/functions-pt147.jsp#3824742\">init() method<\/a>:<\/p>\n<p>&lt;cfscript&gt;<br \/>\nfileAsString = &#8220;&#8221;;<br \/>\nfileReader = createObject(&#8220;java&#8221;, &#8220;java.io.FileReader&#8221;);<br \/>\nfileReader.init(form.upFile);<br \/>\nbufferedReader = createObject(&#8220;java&#8221;, &#8220;java.io.BufferedReader&#8221;);<br \/>\nbufferedReader.init(fileReader);<br \/>\ntry {<br \/>\n&nbsp;&nbsp;&nbsp;do {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileAsString = bufferedReader.readLine();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;processLine(fileAsString);<br \/>\n&nbsp;&nbsp;&nbsp;} while (true);<br \/>\n } catch (coldfusion.runtime.UndefinedVariableException e) {<br \/>\n    \/\/ this indicates end of file, ok to ignore error<br \/>\n }<br \/>\n&lt;\/cfscript&gt;<\/p>\n<p>Basically, you use the CreateObject() function to get a Java object.  At that point, you can call static methods on the object or you can use the init() method to call the constructor of the Java object, which I&#8217;ve done above, ie:<\/p>\n<p>\/\/ get a FileReader object<br \/>\nfileReader = createObject(&#8220;java&#8221;, &#8220;java.io.FileReader&#8221;);<br \/>\n\/\/ call the FileReader object constructor<br \/>\nfileReader.init(form.upFile);<\/p>\n<p>If you&#8217;re curious about this kind of stuff, you should check out the article on <a href=\"http:\/\/java.sun.com\/\">Java<\/a>, <a href=\"http:\/\/www.macromedia.com\/software\/coldfusion\/\">ColdFusion MX<\/a> and <a href=\"http:\/\/jakarta.apache.org\/lucene\/docs\/index.html\">Lucene<\/a> integration I wrote for the July issue of <a href=\"http:\/\/www.sys-con.com\/coldfusion\/\">ColdFusion Developer&#8217;s Journal<\/a>, due to hit the stands anytime now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I mentioned yesterday that Sam posted some great code that lets you upload files to ColdFusion without using CFFILE. His code can be shortened considerably with the use of the init() method: &lt;cfscript&gt; fileAsString = &#8220;&#8221;; fileReader = createObject(&#8220;java&#8221;, &#8220;java.io.FileReader&#8221;); fileReader.init(form.upFile); bufferedReader = createObject(&#8220;java&#8221;, &#8220;java.io.BufferedReader&#8221;); bufferedReader.init(fileReader); try { &nbsp;&nbsp;&nbsp;do { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fileAsString = bufferedReader.readLine(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;processLine(fileAsString); &nbsp;&nbsp;&nbsp;} &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/06\/28\/calling-java-constructors-with-coldfusion\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Calling Java Constructors with ColdFusion<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/441"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=441"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/441\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}