简体   繁体   English

使用coldfusion编写.properties文件

[英]Write .properties file with coldfusion

Does anybody already made this?有人已经做了这个吗? Because it is possible to use the JavaRB.cfc made by mr.因为可以使用先生制作的JavaRB.cfc。 Paul Hastings, but it gives the possibility to read from a properties file, no to write into it? Paul Hastings,但它提供了从属性文件读取的可能性,而不是写入它?

You can use the underlying Java Properties class to do this pretty easily:您可以使用底层 Java Properties 类轻松完成此操作:

<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:尽管属性文件的格式非常简单,因此您也可以使用 ColdFusion 文件函数来编写属性文件:

<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.如果它在结构中,使用 CF 可能更容易。 If it's in a Java Properties object, then the code above is pretty minimal如果它在 Java Properties 对象中,那么上面的代码非常少

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:来自fishwhisprerer的评论如下:

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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