3

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;
Joshua
  • 40,822
  • 8
  • 72
  • 132
Adam James
  • 3,833
  • 6
  • 28
  • 47
  • How do you identify key and values ? You should probably do somethign like this http://stackoverflow.com/questions/23506471/spring-access-all-environment-properties-as-a-map-or-properties-object use List instead of map if you want to – Pragnani Mar 04 '16 at 14:43
  • Turns out the property file is actually just a list of values, no keys – Adam James Mar 04 '16 at 14:50
  • I also don't really want these to be at the environment system level. There are essentially just a bunch of values that we iterate over to deploy certain queues and it is only used in this one location. Is there anything more like I listed above? – Adam James Mar 04 '16 at 14:55

2 Answers2

3

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;
Kishore Bandi
  • 5,537
  • 2
  • 31
  • 52
  • Yea I was thinking of this but its a large project and getting the team to change the property file would be probably 2 months worth of effort that I don't want to spend lol. This is more of a "it would be cool if this exists,does it?" question. My current code change besides reading each line individually is to do the suggested above using apache FileUtils to read all lines at once – Adam James Mar 04 '16 at 15:15
2

Can you not use the following

List<String> list = Files.readAllLines(new File("propertiesFile").toPath(), Charset.defaultCharset() );

PS: This is part of Java 7

The Roy
  • 2,178
  • 1
  • 17
  • 33
  • This is my current suggestion, currently they open an `InputStream` and then create a `Scanner` and then go through each line putting it into a list. I knew of the ability to put a list of properties in a list but just wondering if I could condense this even more – Adam James Mar 04 '16 at 15:13
  • The Sprint Property Source class presents a source of name/value property pairs. The underlying source object may be of any type T that encapsulates properties. Examples include Properties objects, Map objects, ServletContext and ServletConfig objects (for access to init parameters). – The Roy Mar 04 '16 at 15:17
  • 1
    If you are worried about just being condensed and not ready to change the properties to key/value pair, then the above code is a good option. It's a one liner change anyway. – The Roy Mar 04 '16 at 15:20
  • Yea I think this is the best route for now. This whole class that deals with the list of strings is weird to begin with. – Adam James Mar 04 '16 at 17:16