Unable to read the last duplicate key of a Properties file - java

I am trying to read the last value corresponding to a duplicate key in my mail.properties file. The properties file body looks like :
//other key/values properties
mail.body=body1
mail.subject=Reminder
mail.body=body2
mail.body=body3
//other key/values properties
The mail.properties is a result of the concatenation of many properties files at build time. The present evolution requires that I read the last mail.subject and mail.body key/value entries. Thus the normal behavior of my resourceBundle.getString("mail.body") should return body3 but it returns body2 instead. I have double checked if I am reading the right file and it seems that everything is correct. What could be the reason behind this abnormal behavior ?

Related

How can I modify a particular line in a file without deleting the existing data using Java?

I need to change the values in a file which has more than 30 lines and each line has a data like:
ENABLE_TLS=true
PSWD_MIN_LENGTH=8
Here, let us consider this as a key and value pair, and I needed to change only the value for the 2nd line alone, without deleting the 1st line. Can someone help me how can I do this??
I have tried bufferedwriter, but it is replacing all the lines.
My expectation is:
I need to modify only a particular key's value and the remaining lines should not get deleted
Your description of the data sounds like Java Properties. If you are certain that all the data in that file takes the form key=value you could read it in as a Properties object, update the value for the key in question, and write it back to the file.
Properties properties = new Properties();
try (FileInputStream inputStream = new FileInputStream("/path/to/file")) {
properties.load(inputStream);
}
properties.put("PSWD_MIN_LENGTH", 12);
try (FileOutputStream outputStream = new FileOutputStream("/path/to/file")) {
properties.store(outputStream, null);
}
BEWARE: there is no guarantee that the order of the key/value entries in the file will be maintained (they probably won't). If you are looking for a Properties implementation that will maintain the order, maybe this SO answer will do the trick (UNTESTED!) How maintain the order of keys in Java properties file?

How to validate values in a YAML configuration file while loading it?

Is there a way to validate values in a YAML file while loading it in the code. The requirement is I have some elements in the YAML file which must have values. If the validation fails, then YAML should not be loaded.
I'm using snakeyaml library and heard there is a way to do this via Representer.
Code I'm currently using to load the YAML,
Reader in = new InputStreamReader(Files.newInputStream(file), StandardCharsets.UTF_8);
Yaml yaml = new Yaml();
yaml.setBeanAccess(BeanAccess.FIELD);
return yaml.loadAs(in, School.class);
Since you can have any value in a YAML file, you should load the file in a function, test the values and raise an error if the values are not what you want. Return the loaded data if they are.
This may have side-effects if your YAML has tags that create arbitrary objects, but checking during loading will not prevent that, as such object might have been created before you come to the value you want to check.
If you do have tags in your YAML and that is a real problem, then you would have to make a safe_load-er for the YAML file that can handle the tags (by creating normal mapping objects), then check the values and reload with full tag support.

How to read all properties into an array with spring?

I know you can use spring to read a single property, and to read a single property that has a list of values into a list. But what about reading all the properties from a file into a list?
I.E.
EDIT: The property file we are reading is litterally just a list of values, no key, like the updated example below:
Property File
queueName1
quename2
queName3
...etc (the file is like 100 lines long hence why its not a list of values with one property name)
and then be able to do something like
//Imaginary Code
#Value("${GET ALL THE LINES}")
List<String> eachLineOfPropertyFile;
If you would want to do it using Spring alone, then separate each of the value using "," in the property file and make use of spring EL.
Your properties file will be like:
property.values=queueName1,quename2,queName3
And with Spring Value Annotation
#Value("#{'${property.values}'.split(',')}")
List<String> eachLineOfPropertyFile;
Can you not use the following
List<String> list = Files.readAllLines(new File("propertiesFile").toPath(), Charset.defaultCharset() );
PS: This is part of Java 7

keeping array list content separately in a properties file in java

I have the below class which is using google guava map as you can see below , now the only query is that in it a list is been created as you can see but i want to keep this list in a properties file seprately since later on many new items will be added and deleted in this list so i want developer to only change the properties file
Below is the code
final Table<String, String, List<String>> values = HashBasedTable.create();
values.put("Tbon", "cy", Lists.newArrayList("cat1","cat12","cat13","cat14"));
values.put("Tbon", "ype", Lists.newArrayList("rat1","rat2","rat3"));
for (Cell<String, String, List<String>> cell: values.cellSet()){
System.out.println(cell.getRowKey()+" "+cell.getColumnKey()+" "+cell.getValue());
}
now as you can see both the array list are hardcoded here "cat1","cat12","cat13","cat14" and "rat1","rat2","rat3" but i want to keep thsese array list in a properties file seprately so that it can be map against row key so the properties file content will be like lets say name of the properties file is abc.properties stored in my local computer C: drive
cy cat1
cy cat12
cy cat13
cy cat14
ype rat1
ype rat2
ype rat3
so please advise if i keep this mappings in a properties filethen how they will be get called in the data structure values
There is plenty of information on how to write property files on your disk on the internet, e.g. here: http://www.mkyong.com/java/java-properties-file-examples/
The default format assumes one value per key so you'll need some splitting and joining. Here's a simple answer to that issue: Multiple values in java.util.Properties

Load properties file in spring in order

Is there any way to load a properties file in spring in order ? I understand Properties is a Hashtable and maps are unordered. I would like a xml based solution rather than a java based solution. Ideally, this should be configurtable from outside.
Edit:
I mean the contents of the properties file should be read and preserved in the same order as in the properties file. Eg:
fr.wiki=http://fr.wikipedia.org
en.wiki=http://en.wikipedia.org
...
If I read the properties file, fr.wiki should be first, en.wiki should be second and so on.
Getting properties in the order they are written in the properties file is not possible. You can add numbers to your property names:
component.01.key = value1
component.02.key = value2
component.03.key = value3
...
See Pulling values from a Java Properties file in order?

Categories

Resources