I reckon you'll be needing a Java CFX then.


Seemed like a fun 5 minute project so here ya go.....


-----------------


import com.allaire.cfx.*;
import java.io.*;


public class cfReadTxtFile implements CustomTag{
   public void processRequest( Request request, Response response )
throws Exception
   {
  if ( !request.attributeExists("filename") ||
!request.attributeExists("variable") )
  {
     throw new Exception("Missing attributes, filename and variable are
required.");
  }


  String filename = request.getAttribute( "filename" );
  String variable = request.getAttribute( "variable" );
  
  String fileContent = "";
  
     try {
         BufferedReader in = new BufferedReader(new
FileReader(filename));
         String str;
         while ((str = in.readLine()) != null) {
             fileContent = fileContent + str +
System.getProperty("line.separator");
         }
         in.close();
     } catch (IOException e) {
     }  
  
  response.setVariable( variable, fileContent);
  
}
}


-------------------


Call it like so...


<cfx_cfReadTxtFile filename="d:\sites\test\string.cfm" variable="test">


<cfdump var="#test#">


Tested and works perfectly on CF5


I'll mail you the source and the compiled CFX off list aswell.


Craig.

-----Original Message-----
From: Stephenie Hamilton [mailto:[EMAIL PROTECTED]
Sent: 06 October 2003 14:20
To: CF-Talk
Subject: Re:parsing txt file - seems unparsable


Tried this on CF5, and received this error:
Unhandled System exception !

Failed to create JavaVM. JNI_CreateJavaVM returned error code
JNI_ERR (this may indicate an unrecognized option string)

which comes from line:
<cfobject type="JAVA" name="jFR" class="java.io.FileReader"
action="">
any ideas?

BTW, using cffile in CFMX to read the file works great, it
doesn't choke on the null char. Too bad we don't have MX yet on our
corporate servers (sigh).

~~
Stephenie


>This is better though.
>
><cfobject type="JAVA" name="jFR" class="java.io.FileReader"
>action=""> ><cfset jFRobj = jFR.init('d:/sites/test/dates.cfm')>
><cfobject type="JAVA" name="jBR" class="java.io.BufferedReader"
>action=""> ><cfset jBRobj = jBR.init(jFRobj)>
><pre>
><cfscript>
>line = "";
>while ( isDefined("line") ){
> line = jBRobj.readLine();
> if ( isDefined("line") )  {
>  writeoutput(line & "#chr(13)#");
> }
>}
>jBRobj.close();
></cfscript>
></pre>
>
>Seems as soon as we reach EOF, jBRobj.readLine() returns null
(expected)
>and variable 'line' is no longer defined, quite handy in this
case.
>
>
  _____  


[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings]

Reply via email to