unable to read Special Charecter like # from properties file in java - 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

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

Removing escape characters when saving a Path in Properties JavaFX

I am saving the path of a file as using Properties in Java into a config file.
this.adb = adb.getAbsolutePath();
this.prop.setProperty("adb", this.adb);
//save config to project root folder
this.prop.store(new FileOutputStream("config"), null);
The contents of config file after saving are:
adb=C\:\\Program Files\\Genymobile\\Genymotion\\tools\\adb.exe
How do I save it without the escape characters so that it can be easy for a user to modify the path manually without having to type in escape characters.
I also tried to save the config file manually without using escape characters, but the program read the property as:
C:Program FilesGenymobileGenymotion oolsadb.exe
And the code for reading properties from the config file is:
prop.load(new FileInputStream("config"));
this.adb = prop.getProperty("adb");
java.util.Properties.store uses backslash to escape special characters (e.g. equal sign in properties keys), so a literal backslash itself will need to be escaped \\. If this behaviour is not what you want, don't use java.util.Properties, roll out your own utility Properties class it should be a simple exercise. If your not sure what an escape character is check this Wikipedia page.
The relevant code in Properties class (saveConvert method)
if (aChar == '\\') {
outBuffer.append('\\'); outBuffer.append('\\');
continue;
}

Saving special characters in properties file

I have property with string that contains some special characters. After I save it to properties file I have:
BB\u0161BB=0
I don't like character represented with \u0161 . Why it can't save like character I see it on the screen and type from keyboard?
UPD
What is the easiest way to read ini-file architecture file that contains special character?
That's how Properties files are defined to behave. Any other system is likely to use UTF-8 which might not be readable either.
As your character is outside the range of an ISO-8859-1 encoding, it has to use unicode instead.
You can!
Encode your file in unicode (UTF-8, for instance) using another application (Notepad++ is nice, for instance) and read your properties file like this:
File file = ... ;
Properties properties = new Properties();
try (FileInputStream in = new FileInputStream(file);
Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
properties.load(reader);
}
// use properties
There you go, properties file in any charset that you can read and use.
You can't. From javadoc:
...the input/output stream is encoded in ISO 8859-1 character
encoding. Characters that cannot be directly represented in this
encoding can be written using Unicode escapes as defined in section
3.3 of The Java™ Language Specification; only a single 'u' character is allowed in an escape sequence.
Please refer to this How to use UTF-8 in resource properties with ResourceBundle
Properties files are ISO-8859-1 encoded, so characters outside of that set need to be \uXXXX escaped.

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

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

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.

Categories