Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write .properties file with coldfusion

Does anybody already made this? Because it is possible to use the JavaRB.cfc made by mr. Paul Hastings, but it gives the possibility to read from a properties file, no to write into it?

like image 983
Stéphane Vantroyen Avatar asked Dec 13 '22 00:12

Stéphane Vantroyen


1 Answers

You can use the underlying Java Properties class to do this pretty easily:

<cfscript>
fos = CreateObject("java","java.io.FileOutputStream").init(ExpandPath("out.properties"));
props = CreateObject("java","java.util.Properties");

props.setProperty("site","stackoverflow.com");
props.setProperty("for","Stephane");

props.store(fos,"This is a properties file saved from CF");
</cfscript>

Although the format of a properties file is pretty simple, so you could also use the ColdFusion file functions to write a properties file:

<cfscript>
props={"site"="stackoverflow.com","for"="Stephane"};
crlf=chr(13) & chr(10);

propFile = FileOpen(ExpandPath("out2.properties"),"write");
FileWrite(propFile,"##This is a properties file saved from CF" & crlf );
for(prop in props){
    FileWrite(propFile,prop & "=" & props[prop] & crlf);
}
FileClose(propFile);
</cfscript>

It probably comes down to where you have the data stored. If it's in a struct, it may be easier to use CF. If it's in a Java Properties object, then the code above is pretty minimal

like image 189
barnyr Avatar answered Jan 01 '23 06:01

barnyr