User Guide Cancel

GetPropertyString

 

Description

Retrieves the value of a key that is defined in a properties file. 

Returns

The value of the key. 

Syntax

getPropertyString(String filePath, String key, String encoding)

History

  • ColdFusion (2025 release): Added the function 

Parameters

Parameter

 

 

Required

 

 

Type

 

 

Description

 

 

filePath

 

 

Yes

 

 

String

 

 

The absolute path of the properties file. 

 

 

key

 

 

Yes

 

 

String

 

 

A key defined in the properties file. 

 

 

encoding

 

 

No

 

 

String

 

 

The encoding of the properties file. The default is UTF-8. 

 

 

Example

Example 1

<cfscript>
path =GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties"
property1=getPropertyString(path,"appname");
property2=getPropertyString(path,"port");
property3=getPropertyString(path,"smtpserver");
writeDump(property1)
writeDump(property2)
writeDump(property3)
</cfscript>
<cfscript> path =GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties" property1=getPropertyString(path,"appname"); property2=getPropertyString(path,"port"); property3=getPropertyString(path,"smtpserver"); writeDump(property1) writeDump(property2) writeDump(property3) </cfscript>
<cfscript> 
    path =GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties" 
    property1=getPropertyString(path,"appname"); 
    property2=getPropertyString(path,"port"); 
    property3=getPropertyString(path,"smtpserver"); 
    writeDump(property1) 
    writeDump(property2) 
    writeDump(property3) 
</cfscript>

Output

SampleApp 5432 smtp.example.com 

Example 2- with encoding

<cfscript>
path =GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties"
property1=getPropertyString(path,"app.name","UTF-8");
property2=getPropertyString(path,"port","UTF-8");
property3=getPropertyString(path,"smtp.server","UTF-8");
writeDump(property1)
writeDump(property2)
writeDump(property3)
</cfscript>
<cfscript> path =GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties" property1=getPropertyString(path,"app.name","UTF-8"); property2=getPropertyString(path,"port","UTF-8"); property3=getPropertyString(path,"smtp.server","UTF-8"); writeDump(property1) writeDump(property2) writeDump(property3) </cfscript>
<cfscript>
    path =GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties"
    property1=getPropertyString(path,"app.name","UTF-8");
    property2=getPropertyString(path,"port","UTF-8");
    property3=getPropertyString(path,"smtp.server","UTF-8");
    writeDump(property1)
    writeDump(property2)
    writeDump(property3)
</cfscript>

Output

SampleApp 5432 smtp.example.com

Example 3- Empty key

<cfscript>
//Empty Key
try {
path = GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties";
// Ensure that the function getPropertyString is defined or imported properly
property1 = getPropertyString(path,"");
}
catch (any e) {
writeOutput(e.message);
}
</cfscript>
<cfscript> //Empty Key try { path = GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties"; // Ensure that the function getPropertyString is defined or imported properly property1 = getPropertyString(path,""); } catch (any e) { writeOutput(e.message); } </cfscript>
<cfscript>
    //Empty Key
    try {
        path = GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties";
        // Ensure that the function getPropertyString is defined or imported properly
        property1 = getPropertyString(path,"");
    } 
    catch (any e) {
        writeOutput(e.message);
    }
</cfscript>

Output

"Key cannot be empty"

Example 4- key not present or invalid key

<cfscript>
//key not present or invalid key
try {
path = GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties";
// Ensure that the function getPropertyString is defined or imported properly
property1 = getPropertyString(path,"invalid.name");
writeDump(property1)
}
catch (any e) {
writeOutput(e.message);
}
</cfscript>
<cfscript> //key not present or invalid key try { path = GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties"; // Ensure that the function getPropertyString is defined or imported properly property1 = getPropertyString(path,"invalid.name"); writeDump(property1) } catch (any e) { writeOutput(e.message); } </cfscript>
<cfscript>
    //key not present or invalid key
    try {
        path = GetDirectoryFromPath(GetCurrentTemplatePath()) & "test.properties";
        // Ensure that the function getPropertyString is defined or imported properly
        property1 = getPropertyString(path,"invalid.name");  
        writeDump(property1)      
    } 
    catch (any e) {
        writeOutput(e.message);
    }
</cfscript>

Output

[empty string]

Get help faster and easier

New user?