Skip to content

Profiler Example : Read a file, Create a new file and add a new lines

The Read_Create_Write_Example.cfmis an example where the script reads a file named myFile1.txt and after that creates a new file called myFile2.txt and adds a number of new lines.


        <title>This is a read/write example of ColdFusion process</title>
        <cffile action="read" file="/opt/applicationservers/coldfusion/11.0/cfusion/wwwroot/Test/myFile1.txt" variable="Message">
        <cffile action="write" file="/opt/applicationservers/coldfusion/11.0/cfusion/wwwroot/Test/myFile2.txt" output="new line">
        <cfloop from="1" to="200000" index="i">
            <cfset s = gettickcount()>
            <cffile action="append" file="/opt/applicationservers/coldfusion/11.0/cfusion/wwwroot/Test/myFile2.txt" output="Did you add something?">
            This is the execution time: #s#  
        <h2> This is the end of the loop.</h2>

Lets run that script and see the output in the Profiler.

Profiler Results

When the script has been executed, we are going to find the transaction in the Profiled Thread History tab. ( See screenshot below )

Now lets take a deeper look into the transaction. ( See screenshot below )

By viewing the screenshot, we can see that 75.1% of the total duration time was consumed by the function that was used for appending the new lines to myFile2.txt. The appendToFile function took that amount of time because we are appending 19,999 new lines to the myFile2.txt file. The next most consuming method was the that was responsible for writing 19,999 new lines in the myFile2.txt file.