简体   繁体   中英

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?

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

Should anybody stumble across this page in the future, I've also found how to read properties from this source .

The comment from fishwhisprerer states as following:

Using Java in this scenario could be more efficient:

#myproperties
key=value

<cfscript>
props = createObject("java","java.util.Properties");
inputStream = createObject("java","java.io.FileInputStream").init(expandPath("my.properties"));
props.load(inputStream);
</cfscript>

Then anywhere below this:

props.getProperty("key")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM