Java .properties read Strings with the same name
Ok so I got a .properties file in my Project and now, I would like to read every single property, which starts with "API" in this example and return it as a list or something, how can I do that
31 Replies
⌛
This post has been reserved for your question.
Hey @netty.bootstrap! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
You can either iterate over all keys in properties and filter all that start with "API", or use streams
Depends how you are currently reading your properties
like this
and resources
So you have a
Properties
object, right?yes
Okay, sec
So basically, you want to read all properties that start with specified string and store it in a list right? So in this example you expect to get a list containing just the api links?
yes
indeed
So you have two options. Let me start with the one that is fairly simple to understand.
So, you can iterate over all entries in your Properties object
Properties
extends a map, so it also has an entrySet
method, which returns a set of entries in this properties object
You can then check every key one by one if it starts with desired string ("API" in your case), and if it does, just add a value associated with current entry to a List created beforeok I see
You should do
instanceof
checks, because Properties#entrySet()
returns a set of <Object, Object>
pairs, so it's safer to check if these Object
s are String
s for sureOk I see, now what do I call or use to uhm write for example the first API thing into a string or something
like where will the links be saved
In this example it will be the
api_links
List, it should contain all links read from Properties
api_links[]
ojk ye
I can just iterate through that list using .length right
size()
, but yeahok
do I need .getProperty also or no
No
Cuz its already saved in the list right
Yeah
ok
This list is completely independent from the
Properties
objectok good
ok thats it I guess
thanks men
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
You are welcome
uh another question rq
How do I create such a list, with as much elements as a method for example needs
cuz I got a vars.java file where all read properties are saved as static
would this work
Depends on what you want to achieve, but if your file looks roughly like this:
then that should work. You will be able to access the same list by just calling
Vars.apis
ok, will Vars.apis return single strings or
god ive never used this, that shit is breaking my brain
It returns the very same List you passed with
setApis
setApis just assigns your list to a variable in the classok it wokred
fire
ok for now ill try to work w that
thanks mate
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.