I have a config properties file in a java project created in eclipse.
Below are the contents in the properties file.
adminApp= testAdminDemo
customerApp = testCustDemo
appHostIP = 172.22.XX.XX
adminAppURL = http://appHostIP:9049/adminApp
customerAppURL = http://appHostIP:9049/customerApp
When the appURL parameter is read from a Java class,
Properties prop = new Properties();
FileInputStream f = new FileInputStream(System.getProperty("user.dir") + "\\config.properties");
prop.load(f);
String url = prop.getProperty("appURL");
System.out.println("URL: " + url);
the output is the same value mentioned for parameter 'url' in config.parameters file:
http://appHostIP:9049/adminApp
Actually, I expected the output to be like:
http://172.22.XX.XX:9049/testAdminDemo
Is there anything wrong in this approach?
I don't want to read host ip and app name in the java class file and then form a string. Instead, the required URl should get formed in the properties file as need to deal with different apps - admin, customer, etc.
You cannot perform concatination in the property file, You have to handle the concatination in code where you are using it like below.
appHostIP = 172.22.XX.XX
adminAppURL = :9049/adminApp
String url = "http://" + "prop.getProperty("appHostIP")+ prop.getProperty("appURL");
i have in some spring application , in application-lcl.properties a line with :
key1=value1
I want to use the value of key1 in another xml like this :
<appender name="ELASTIC" class="com.internetitem.logback.elasticsearch.ElasticsearchAppender">
<url>${key1}</url>
${key1} doesn't work. Do you know how to do it ? (the .xml already exists )
Thanks
Its a 2 step process
Load properties file into java.util.java.util.Properties class object.
Use Properties.storeToXML() method to write the content as XML
String inPropertiesFile = "application.properties";
String outXmlFile = "applicationProperties.xml";
InputStream is = new FileInputStream(inPropertiesFile); //Input file
OutputStream os = new FileOutputStream(outXmlFile); //Output file
Properties props = new Properties();
props.load(is);
props.storeToXML(os, "application.properties","UTF-8");
in the xml , put
<springProperty name="value1" source="key1"/>
and then use it by calling
<url>${value1}</url>
I am reading a property file which consists of a message in the UTF-8 character set.
Problem
The output is not in the appropriate format. I am using an InputStream.
The property file looks like
username=LBSUSER
password=Lbs#123
url=http://localhost:1010/soapfe/services/MessagingWS
timeout=20000
message=Spanish character are = {á é í, ó,ú ,ü, ñ, ç, å, Á, É, Í, Ó, Ú, Ü, Ñ, Ç, ¿, °, 4° año = cuarto año, €, ¢, £, ¥}
And I am reading the file like this,
Properties props = new Properties();
props.load(new FileInputStream("uinsoaptest.properties"));
String username = props.getProperty("username", "test");
String password = props.getProperty("password", "12345");
String url = props.getProperty("url", "12345");
int timeout = Integer.parseInt(props.getProperty("timeout", "8000"));
String messagetext = props.getProperty("message");
System.out.println("This is soap msg : " + messagetext);
The output of the above message is
You can see the message in the console after the line
{************************ SOAP MESSAGE TEST***********************}
I will be obliged if I can get any help reading this file properly. I can read this file with another approach but I am looking for less code modification.
Use an InputStreamReader with Properties.load(Reader reader):
FileInputStream input = new FileInputStream(new File("uinsoaptest.properties"));
props.load(new InputStreamReader(input, Charset.forName("UTF-8")));
As a method, this may resemble the following:
private Properties read( final Path file ) throws IOException {
final var properties = new Properties();
try( final var in = new InputStreamReader(
new FileInputStream( file.toFile() ), StandardCharsets.UTF_8 ) ) {
properties.load( in );
}
return properties;
}
Don't forget to close your streams. Java 7 introduced StandardCharsets.UTF_8.
Use props.load(new FileReader("uinsoaptest.properties")) instead. By default it uses the encoding Charset.forName(System.getProperty("file.encoding")) which can be set to UTF-8 with System.setProperty("file.encoding", "UTF-8") or with the commandline parameter -Dfile.encoding=UTF-8.
If somebody use #Value annotation, could try StringUils.
#Value("${title}")
private String pageTitle;
public String getPageTitle() {
return StringUtils.toEncodedString(pageTitle.getBytes(Charset.forName("ISO-8859-1")), Charset.forName("UTF-8"));
}
You should specify the UTF-8 encoding when you construct your FileInputStream object. You can use this constructor:
new FileInputStream("uinsoaptest.properties", "UTF-8");
If you want to make a change to your JVM so as to be able to read UTF-8 files by default, you will have to change the JAVA_TOOL_OPTIONS in your JVM options to something like this :
-Dfile.encoding=UTF-8
If anybody comes across this problem in Kotlin, like me:
The accepted solution of #Würgspaß works here as well. The corresponding Kotlin syntax:
Instead of the usual
val properties = Properties()
filePath.toFile().inputStream().use { stream -> properties.load(stream) }
I had to use
val properties = Properties()
InputStreamReader(FileInputStream(filePath.toFile()), StandardCharsets.UTF_8).use { stream -> properties.load(stream) }
With this, special UTF-8 characters are loaded correctly from the properties file given in filePath.
I want to read data from an XML file. I am using Java & Selenium WebDriver. I have found many solutions while researching. The problem is none seems to apply to my problem.
My XML file is as such :
<Enviroment>
<Parameter>Test_Url</Parameter>
<value>https://www.google.com</value>
<Parameter>Distributed_Test</Parameter>
<value>no</value>
<Parameter>Result_Name</Parameter>
<value>Google_Results</value>
</Enviroment>
The code I am using to read this xml file is here.
public class ReadXML {
static String value;
public static void main(String[] args) throws FileNotFoundException,IOException {
File file = new File("path of the file");
FileInputStream fileInput =new FileInputStream(file);
Properties prop =new Properties();
//prop.load(fileInput);
prop.loadFromXML(fileInput);
fileInput.close();
Enumeration enumKeys=prop.keys();
while(enumKeys.hasMoreElements()){
//String node = "Environment";
String subnode= "Parameter";
if(((String) enumKeys.nextElement()).contains(subnode)){
value = prop.getProperty(subnode);
System.out.println(value);
}
}
return ;
}
When I am using prop.load(fileInput), output is printed as null thrice for the three parameter values, I believe.
But if I use prop.loadFromXML(fileInput), InvalidPropertiesFormatException is shown.
Please help..Thanks in Advance!!
The error in the properties format can be checked via xmllint:
xmllint foo.properties
In this case, the closing tag is:
</Enviroment>
but needs to be:
</Environment>
References
Class Properties
# Compulsory Dimension to create port
xOffset=-3
yOffset=50
How to get these xOffset and YOffset in java file.I tried with inputstream but not getting.These variable should get loaded in java file.
You can use Properties class from Java library
Properties prop = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(inputStream);
The you can get the values as
prop.getProperty("propertyname");
Try the following code:
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("your_config.properties");
prop.load(input);
System.out.println(prop.getProperty("xOffset"));
System.out.println(prop.getProperty("yOffset"));
} catch (IOException e) {
// ...
}
As above explained Create a Function to read the property File During or Before Selenium Driver Constructor . So you can use them in test ( Help in Desire Capability Impl).
Store the values in Public Static final ( If you don not want to change them in Selenium and use as Default Property input)
As the values are read by Java Propertie file or .config file before selenium Driver so you can use them in Driver constructor or if you don't want you can use those properties stored as Static anywhere in the project. These values act as GLOBAL param.