Why a java property not working when starts with "!"? - java

I tried this properties file:
# test config
# global configuration
!title=Test report
!background=fff
query=...
paramSource=...
but !param and !background are never read.
Why?

From the Javadoc for Properties
A comment line has an ASCII '#' or '!' as its first non-white space character

! is a special character for create a comment and will be ignored.

Related

Java - Escaped backslashes being taken literally when writing to file

I want to store a URL in a properties file. This is the URL:
jdbc\:sqlserver\://dummydata\\SHARED
When programming this in Java, I obviously need to escape the backslashes. So my code ends up looking like this
properties.setProperty("db", "jdbc\\:sqlserver\\://dummydata\\\\SHARED");
The issue with this is that the properties file is saving the String URL and including the backslashes used for escaping, which is an incorrect URL. I was hoping that Java would interpret the backslashes used for escaping so that only the correct URL is saved. Is there a way to achieve this?
You're correct that a property value with : needs to escape the colons in a .properties text file, but you're not writing that text file directly.
You are giving the value to a Properties object using setProperty(), and presumably writing that to a text file using store(), and the store() method will escape the values as needed for you.
You should give the value you want to Properties, and forget about the encoding rules of the text file. Properties will handle all needed encoding. Since the value you want to give is jdbc:sqlserver://dummydata\SHARED, you write a string literal "jdbc:sqlserver://dummydata\\SHARED"
Example
String db = "jdbc:sqlserver://dummydata\\SHARED";
System.out.println(db); // To see actual string value
Properties properties = new Properties();
properties.setProperty("db", db);
try (FileWriter out = new FileWriter("test.properties")) {
properties.store(out, null);
}
Output
jdbc:sqlserver://dummydata\SHARED
Content of test.properties
#Tue Jun 11 11:54:24 EDT 2019
db=jdbc\:sqlserver\://dummydata\\SHARED
As you can see, the store() method has escaped the : and \ for you.
If you save the properties as an XML file instead, there's no need to escape anything, and Properties won't.
Example
try (FileOutputStream out = new FileOutputStream("test.xml")) {
properties.storeToXML(out, null);
}
Content of test.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="db">jdbc:sqlserver://dummydata\SHARED</entry>
</properties>
Properties.store() escapes backslashes, there is no way around it. I guess my first question is why is this an issue? Are you reading the file in any other way than using Properties.load(). If not they you don't need to worry about it as the load function will remove the escape characters.
properties.load(file);
System.out.println(properties.get("db"));
// output: jdbc\:sqlserver\://dummydata\\SHARED
As an aside are you sure you the URL is correct? Shouldn't you be storing it as properties.setProperty("jdbc:sqlserver://dummydata\SHARED")?
In the documentation for load, it says the following:
The method does not treat a backslash character, \, before a non-valid escape character as an error; the backslash is silently dropped. For example, in a Java string the sequence "\z" would cause a compile time error. In contrast, this method silently drops the backslash. Therefore, this method treats the two character sequence "\b" as equivalent to the single character 'b'.
This means that two backslashes will be treated as a single one because it's not a valid escape sequence. Loading this string should work just fine:
C:\\path\\to\\file

unable to read Special Charecter like # from properties file in java

How to read Special Character # from Properties file in Java
password=Admin123#
when i read read password from properties file i am getting
password=Admin123
You need to escape special characters: How to escape the equals sign in properties files see answer 4 exactly for your case:
https://stackoverflow.com/a/33808156/3453727

Not able to read Properties File using Backward Slash

I Am not able to read Properties File using Java.It Means In this Properties File Backward Slash is not working.It is showing like ,this destination :C:Usersxxx.a
String filename="D://Desktop//xxx.properties";
is = new FileInputStream(filename);
Properties prop=new Properties();
prop.load(is);
System.out.println("destination :"+prop.getProperty("destination"));
Property File is the :
destination=C:\Users\xxx.a\
Result is showing
destination :C:Usersxxx.a
But I want to show destination :C:\Usersxxx.a\
Can You Please suggest Me?
\ is an Escape character.
forward slash / is used as path separator in Unix environment.
Back slash \ is used as path separator in Windows environment.
So, You need to use \\ or / as path separator. You can not directly use \ in java. Since, it is an escape character.
So,You need to make changes in your properties file to make your program work.
Use either / or \\ as path separator in your properties file.
In your case you want to show as C:\Users\xxx.a\.
So, use C:\\Users\\xxx.a\\ in your properties file to get output as C:\Users\xxx.a\
The \ character is used as an "escape character" in many programming languages. It gives a special meaning to the next character in the text. For example, \n encodes the special character "new-line".
Use \\ instead of \. This indicates to the parser that you mean the actual symbol, not an escape character. For example, your property value would be:
destination=C:\\Users\\xxx.a\\
You need to add two slashes to your properties file like this:
destination=C:\\Users\\xxx.a\\
The other way is to swap the slashes in the properties file:
destination=C:/Users/xxx.a/
A \ is an escape character so it is removed. Adding two slashes escapes the first so only one is left.
You can store it in D:/Desktop/xxx.properties as
destination=C:/Users/xxx.a/
and show it with a single backslash
String fileName = prop.getProperty("destination");
System.out.println("destination: " + fileName); // shows: C:/Users/xxx.a/
System.out.println("destination: " + Paths.get(fileName)); // shows: C:\Users\xxx.a

how to replace a backslash in some lines of a text in Ant

Using
<replaceregexp byline="true" flags="g" file="${someFIle}"
match = "[\\]"
replace = "/"/>
I can easily replace all the \ with /.
But
Suppose the text is:
Other A\B\C
Some C\D\E
Other ...
Other ...
...
How to replace the "\" with "/" in the second line which has prefix "Some", but not other lines:
Other A\B\C
Some C/D/E
...
replaceregexp does not have options to skip lines.
Your problem cannot be merely solved by a single regex. You need to do something more complex.
1) Read properties only with the specified prefix and save them to variable. This can be done with propertyregex.
2) Read all other properties and save them somewhere.
3) Replace \ with whatever you want in the properties which contain the prefix.
4) Write the modified properties with the properties without the prefix into a file replacing the original file.
5) Enjoy :-)
Try to do these steps one by one. If you still have questions let me know.

Regex to remove comments in XML file in Eclipse java

Can anyone tell me how to write a regular expression to match the comments in XML file, for example,
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
I'm using Eclipse (java) and want to remove those comments from XML file.
xmlFileContent.replaceAll( "(?s)<!--.*?-->", "" );
xmlFileContent.replaceAll("<!--[\s\S]*?-->", "");
[\s\S] works like '.', but will consume line endings as well.
xmlFileContent.replaceAll("<!--.*?-->", "");

Categories