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.
Related
I need a regex expression that will select files with particular file name format from the file list of properties files.
I need to select files with file names with following file format:
<app_name>_<app_version>_<environment>.properties
here <app_name> can be any alphanumeric with special character <A-Z/a-z/0-9/special char> like abc123 or app1-1
here <app_version> can be any alphanumeric with special character <A-Z/a-z/0-9/special char/float value> like abc or even float/integer/string 1.0 or 2 or abc1
here <environment> can be any alphanumeric with special character <A-Z/a-z/0-9/special char> like production or prod1
Together they are bind with 2 underscore as follows:-
<A-Z/a-z/0-9/special char>_<A-Z/a-z/0-9/special char/float value>_<A-Z/a-z/0-9/special char>.properties
The file name always contains 2 underscore _, and it can be any string between the underscores .
for examples, following are valid file names that can be selected:
app1_1.0_prod1.properties
app2_2_prod2.properties
app_vers1_prod.properties
app-1_vers1_prod-2.properties
asd_efg_eee.properties
It can be letter or number or special char or combination between them, between the underscore .
Please note that it can be only 2 underscore _ in the file name.
Anything other than 2 underscore _ is not a valid file name and will not be selected and the file name should always have these 3 sections separated by 2 underscore _
Following are invalid file name:
abc.properties
abc.123.efg.properties
as_1.efg.ddd.rr.properties
ee_rr.properties
_rr_.properties
I tried following regex:
[^_]*\\.[^_].properties
but not working. Maybe this is wrong. I am not getting the clue of getting this.
Please help me in creating this regex.
Thanks
I believe /^[^_]+_[^_]+_[^_]+\.properties$/ should meet your requirements:
const tests = [
'app1_1.0_prod1.properties',
'app2_2_prod2.properties',
'app_vers1_prod.properties',
'asd_efg_eee.properties',
'abc.properties',
'abc.123.efg.properties',
'as_1.efg.ddd.rr.properties',
'ee_rr.properties',
'_rr_.properties'
];
tests.forEach(test => {
console.log(test, /^[^_]+_[^_]+_[^_]+\.properties$/.test(test));
});
Alternatively, you could use /^([^_]+_){2}[^_]+\.properties$/
If you want to tighten down on the use of ., then I think you want
/^[^_.]+_([^_.]+|\d+(\.\d+)?)_[^_.]+\.properties$/
This should work, since each section can include basically any character except an underscore: /[^_]*_[^_]*_[^_]\.properties/
I am working with a coverity issue which i need to validate a file name
using regEx in java . In my application support .pdf , .txt , csv etc . My
file name getting as xxx.txt from user . i want to validate my file name
with proper extension format and not included any special character other
than dot ( eg .txt) .
filePath = properties.getProperty("DOCUMENT.LIBRARY.LOCATION");
String fileName = (String) request.getParameter("read");
Only If the file path is completed itsproper validation, the below code should be work .
filePath += "/" + fileName;
This is a terrible answer as it only verifies the filename ends with the desired extension, but doesn't verify the rest of the filename as requested in the original question. Something more like this would be MUCH better:
fileName.matches("[-_. A-Za-z0-9]+\\.(pdf|txt|csv)");
This ensures the filename contains only ONE OR MORE -, _, PERIOD, SPACE, or alphanumeric characters, followed by exactly one of .pdf, .txt or .csv at the end of the filename. Your system might allow other characters in filenames and you could add them to this list if desired. An alternate, less secure approach is to prevent 'bad' characters something like:
fileName.matches("[^/\]+\\.(pdf|txt|csv)");
Which simply prevents / or \ characters from being in the file name before the required ending extension. But this doesn't prevent potentially other dangerous characters, like NULL bytes, for example.
Have a look at String.endsWith() method
if (fileName.endsWith(".pdf")) {
// do something
}
Or use the method String.matches()
fileName.matches("\\.(pdf|txt|csv)$")
My requirement was to update a key/value pair property file for which Commons Configuration is used.
But problem is when you save any text using this api it remove space after comma in a value.
If you disable parsing then it create multiple keys of safe name broken by comma :(
PropertiesConfiguration config = new PropertiesConfiguration("prop.properties");
//config.setDelimiterParsingDisabled(true);
config.save();
Expected value (with no truncation of spaces after before comma):
Name = some , Text , for testing
If setDelimiterParsingDisabled is false then below is outout all spaces gone
Name = some,Text,for testing
If that is True then below is output
Name = some
Name = Text
Name = for testing
I need first one with all space intact means key as is...how to do that
I believe both things cannot be achieved so what is id is I changed Delimeter to carrot ^ sign and not it behaving as I like.
So this is answered.
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("\\\\", "\\\\\\\\");
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