Java properties value ending in backlash - java

I have a value in a Java .properties file that needs to end in a backlash. The property value should be "\\server\folder\", and I enter the value like so:
name=\\\\server\\folder\\
The trailing backslash is killing whatever property comes on the next line. Am I escaping this incorrectly?

Not sure what the problem is in your case, but this snippet
Properties props = new Properties();
props.load(new FileInputStream("filename.txt"));
System.out.println(props);
Prints
{prop3=val3, prop2=val2\, prop1=val1}
If filename.txt contains
prop1=val1
prop2=val2\\
prop3=val3
Note that a single (or actually, an odd number) of \ in the end of a property line would escape the newline character and things gets messed up.

Related

Escape special characters in properties file java

I have key, value in properties file like this proj.path=${HOME}/dir. I have both environment variable and directory also with ${HOME}.
In my case I would like to use it as directory path only but when I read this from file it is getting replaced with environment variable value (home/user/dir).
I tried to escape it like proj.path=\\$\\{HOME\\}/dir but in code it is coming like \$\{HOME\}/dir
Required output is ${HOME}/dir.
EDIT:
Prop file:
proj.path=${HOME}/dir,some/dir/dir2
I am accessing in spring like below.
#Value("#{'${proj.path}'.split(',')}")
private List<String> customPaths;
One way of escaping the ${HOME} value is by wrapping the $ character in an expression and changing the type from List<String> to String[].
proj.path=#{'$'}{HOME}/dir,some/dir/dir2
#Value("${proj.path}")
private String[] customPaths;

How do I add a line break / blank line after my LOG statement?

I want to add a blank line after my LOG statement in order to make my logs more separated and readable.
How do I do this?
Current statement:
LOGGER.info("Person's name is {} .", person.getName());
Note that I don't want to do this after every statement, just for certain statements.
Simply add \n at the end of the string to log.
LOGGER.info("Person's name is {} .\n", person.getName());
If you are in a windows environment use \r\n
To get the right value of new line if you don't know the end operating system of your application you can use the property line.separator
String lineSeparator = System.getProperty("line.separator");
LOGGER.info("Person's name is {} .{}", person.getName(), lineSeparator);
dont use \n character, but ask the System what is the newline propery. could be different in linuxor windows or..
so use
System.lineSeparator();
or before java 7 use
System.getProperty("line.separator");

Reading path from ini file, backslash escape character disappearing?

I'm reading in an absolute pathname from an ini file and storing the pathname as a String value in my program. However, when I do this, the value that gets stored somehow seems to be losing the backslash so that the path just comes out one big jumbled mess? For example, the ini file would have key, value of:
key=C:\folder\folder2\filename.extension
and the value that gets stored is coming out as C:folderfolder2filename.extension.
Would anyone know how to escape the keys before it gets read in?
Let's also assume that changing the values of the ini file is not an alternative because it's not a file that I create.
Try setting the escape property to false in Ini4j.
http://ini4j.sourceforge.net/apidocs/org/ini4j/Config.html#setEscape%28boolean%29
You can try:
Config.getGlobal().setEscape(false);
If you read the file and then translate the \ to a / before processing, that would work. So the library you are using has a method Ini#load(InputStream) that takes the INI file contents, call it like this:
byte[] data = Files.readAllBytes(Paths.get("directory", "file.ini");
String contents = new String(data).replaceAll("\\\\", "/");
InputStream stream = new ByteArrayInputStream(contents.getBytes());
ini.load(stream);
The processor must be doing the interpretation of the back-slashes, so this will give it data with forward-slashes instead. Or, you could escape the back-slashes before processing, like this:
String contents = new String(data).replaceAll("\\\\", "\\\\\\\\");

Why Properties.store delimiting with \ for values separating with :

I have a file which contains properties like :
MyKey=value1:value2
I am using Properties.load to load these into a property object and then outputting the values into another file (using Property.store ).
But the new file is delimiting it with \
MyKey=value1\:value2
Why is this happening ?
This happens, because : is like = a reserved char.
Truth = Beauty
Truth:Beauty
Truth :Beauty
All these lines will set the value for the Property with the Key Truth to Beauty
http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.Reader)
The write method will escape the : sign to \:. After loading this chars will be removed.

apache commons configuration loads property until "," character

I want to load configuration (apache commons configuration) from a properties file. My program is:
PropertiesConfiguration pc = new PropertiesConfiguration("my.properties");
System.out.println(pc.getString("myValue"));
In my.properties I have
myValue=value,
with comma
When I run program the output is value, not value, with comma. Looks like value is loaded until , character.
Any ideas?
That behavior is clearly documented, i.e., that PropertiesConfiguration treats a value with a comma as multiple values allowing things like:
fruit=apples,banana,oranges
to be interpreted sensibly. The fix (from the doc) is to add a backslash to escape the comma, e.g.,
myKey=value\, with an escaped comma
Check Javadoc. You have to setDelimiterParsingDisabled(true) to disable parsing list of properties.
Actually propConfig.setDelimiterParsingDisabled(true) is working, but you must load the config file after this setting, for example:
propConfig = new PropertiesConfiguration();
propConfig.setDelimiterParsingDisabled(true);
propConfig.load(propertiesFile);
Settings won't work if your code like is:
propConfig = new PropertiesConfiguration(propertiesFile);
propConfig.setDelimiterParsingDisabled(true);
PropertiesConfiguration interprets ',' as a value separator.
If you put \ before the ,, you escape it, and you can read the value
Example:
myValue=value\, with comma
You read = value, with comma without problems

Categories

Resources