File Reading

July 21, 2004 · 2 Comment s

We alll know that we can use <CFFILE> to read in the contents of a file. However, what if you're on a shared hosting account and your host has disabled access to the <CFFILE> tag? You can use Java to get around this. Of course, the only issue you've got here is if your host has also disabled <CFOBJECT> and <createObject> - in which case, go get a new hosting account with someone better. You can use the following code (borrwed nicely from Aaron Johnson, with only a very minor change):
<cfscript>
fileAsString = "";
fileToRead = "d:/www/wwwroot/test/test/testFile.txt";
fileReader = createObject("java", "java.io.FileReader");
fileReader.init(fileToRead);
bufferedReader = createObject("java", "java.io.BufferedReader");
bufferedReader.init(fileReader);
try {
   do {
      fileAsString = bufferedReader.readLine();
      processLine(fileAsString);
   } while (true);
} catch (coldfusion.runtime.UndefinedVariableException e) {
// this indicates end of file, ok to ignore error
}
</cfscript>

<cffunction name="processLine">
   <cfargument name="line" required="true">
   <cfoutput>#arguments.line#<br /></cfoutput>
</cffunction>
In Aaron's example, the code was taking the file from a file upload, but here we're reading in from a file on disk. The key thing to remember is to provide the full path to the file. The other benefit you have with using this method to read in (and then output) the contents of the file is that it is done line by line, whereas <CFFILE> loads the entire file into memory at one time.

Tags: ColdFusion · Java

2 response s so far ↓

  • 1 Vor // Nov 29, 2004 at 6:53 PM

    I tried to use your code to read from bufferedReader but instead of a file, I'm using an input stream from a socket. It is unable to find eof and just hangs there. Here's what I'm working with. Its a news server reader.

    &lt;cfscript&gt;
       /*#######################################################
       DECLARE OBJECTS
       #######################################################*/
       //Create TCP/IP Socket and Attempt a Connection to a News Server
       ngSocket = CreateObject(&quot;Java&quot;,&quot;java.net.Socket&quot;).init(&quot;news.microsoft.com&quot;,&quot;119&quot;);
       
       //Create object to Translate the Bytes recieved to Text
       inStream = CreateObject(&quot;Java&quot;,&quot;java.io.InputStreamReader&quot;).init(ngSocket.getInputStream());
       raw = ngSocket.getInputStream();

       //Create Object to Read and Return text from Input Stream
       read = CreateObject(&quot;Java&quot;,&quot;java.io.BufferedReader&quot;).init(inStream);
       
       //Create Object to Write Text and convert to bytes and send to Server
       write = CreateObject(&quot;Java&quot;,&quot;java.io.PrintWriter&quot;).init(ngSocket.getOutputStream());

       /*#######################################################
       COMMUNICATE WITH SERVER
       #######################################################*/
       WriteLine(&quot;Connection Established&quot;);
       
       //Get initial connection message.

    try {
    do {
    data = read.readLine();
    writeline(data);
    } while (true);
    } catch (coldfusion.runtime.UndefinedVariableException e) {
    // this indicates end of file, ok to ignore error
    }

       /*#######################################################
       CLEANUP
       #######################################################*/
       write.close();
       read.close();
       inStream.close();
       ngSocket.close();
       
       WriteLine(&quot;Cleanup Complete&quot;);
    &lt;/cfscript&gt;

    &lt;cffunction name=&quot;Dump&quot; access=&quot;public&quot;&gt;
       &lt;cfargument name=&quot;v&quot; required=&quot;yes&quot; type=&quot;any&quot;&gt;
       &lt;cfdump var=&quot;#v#&quot;&gt;
    &lt;/cffunction&gt;
    &lt;cffunction name=&quot;WriteLine&quot;&gt;
       &lt;cfargument name=&quot;v&quot; required=&quot;yes&quot; type=&quot;any&quot;&gt;
       &lt;cfset writeoutput(v &amp; &quot;&gt;br&lt;&quot;)&gt;
    &lt;/cffunction&gt;
  • 2 ed hardy mens Jeans // Sep 17, 2009 at 8:43 AM

    Thank you!

Leave a Comment

Leave this field empty: