Using StringBuffer With CFFILE

102 views
Skip to first unread message

Doug Roberson

unread,
Sep 3, 2015, 8:01:30 AM9/3/15
to Lucee
Good morning!

This is not a critical issue - I have the function working using CFFILE's append method. I'm mainly interested in learning the right way to do this sort of thing for the future. 

When I was Googling information on using CFFILE, I came across a 2007 blog entry from Ben Nadel that showed a more efficient way to build the CSV and I wanted to try it out. 


When I attempt to use a stringBuffer created object (tostring) as the CFFILE output, Lucee returns "Can't cast Complex Object Type Query to String, Use Built-In-Function "serialize(Query):String" to create a String from Query" 

It returns this message even when using serialize.

The object works as expected when dumping or writing to cfoutput.

My environment is:

Test server running Lucee 4.5.1.023 on Windows Server 2012 R2, IIS 8
Production server running Lucee 4.5.1.022 on Debian (Hostmedia.co.uk VPS)

Also, I have AFC 10 and 11, both running on Server 2012 R2, at the day job and usually cross-test there.

I've attached two files. 

1) sbuffer.cfm is from my AFC servers and shows that the basic functionality works there - I know that compatability varies by function, just showing how it works. 

2) javapickSchool.cfm is from Lucee. I've included a copy of the query functions I call from the CFC, a few remarks about what the code is doing, and I've also commented out variations that did not work.

Please keep in mind that I'm an experienced tester but a novice programmer, so if I'm doing something stupid, you'll help me a lot by pointing it out.

Also, let me know if you need any more information!






sbuffer.cfm
javapickSchool.cfm

Michael Offner

unread,
Sep 3, 2015, 3:13:22 PM9/3/15
to lu...@googlegroups.com
Can you show me the code you are running, the exception is about a query that cannot be translated to a string. So something else seems off.
From my point of view the example from Ben is not very good (sorry Ben) for 2 reasons.
1. Stringbuffer is fine inside Java (stringbuilder would be even better in this case), but inside CFML you have the problem that every call of the function "append" is using reflection what is not very fast, so you are even faster with simple string concatenation.
2. You store everything in memory, writing it directly into the file is a much more memory efficient.

In your case I would solve this as follows:
file=fileOpen(path2file,"append");
try {
   loop query=qry {
...
       FileAppend(file,text);
   }
}
finally {
   FileClose(file);
}

Micha
--
See Lucee at CFCamp Oct 22 & 23 2015 @ Munich Airport, Germany - Get your ticket NOW - http://www.cfcamp.org/
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/e7c988d3-72a1-48eb-a9eb-71f95f161a19%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Doug Roberson

unread,
Sep 3, 2015, 3:28:17 PM9/3/15
to Lucee
Thanks, Micha!

I will use your recommendation.

To answer your request - the code that doesn't work is in the attachment named "javapickSchool.cfm" 

Michael Offner

unread,
Sep 4, 2015, 3:35:34 AM9/4/15
to lucee
Hi Doug

i have reviewed your code and i think i have found the problem, i cannot execute the code because i miss the enviroment so i'm not a 100% sure, but 99% ;-).
The exception says that the failure is happening on line 66, what would be this line:
        <cffile action            ="WRITE" file="#getExportSchoolName#" output="#swapNames#">
you assumed that "swapNames" is the problem (assumptions can be our biggest enemy ;-) ), but in my opinion the problem is 
"getExportSchoolName" so the value for the attribute "file" what need to be a string (as the exception indicate).
on line 21 you do this:
<cfset exportFileName         = getExportSchoolName.ClassName>

so "getExportSchoolName" is for sure not a string, as the exception indicates it could be a query.
should the value for the attribute "file" be "exportFileName"?

hope this helps
Micha







--
See Lucee at CFCamp Oct 22 & 23 2015 @ Munich Airport, Germany - Get your ticket NOW - http://www.cfcamp.org/
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.

Doug Roberson

unread,
Sep 4, 2015, 6:57:35 AM9/4/15
to Lucee
You, sir, are a wizard, making fire for cavemen, and I thank you.

I was totally fixated on the output and never considered that the problem could be file path or name.

Thank you!


On Thursday, September 3, 2015 at 8:01:30 AM UTC-4, Doug Roberson wrote:
Reply all
Reply to author
Forward
0 new messages