I am quite new to java and trying to print all the system properties with the following code:
This prints all the properties as expected but I am wanting to put each property on a new line and am struggling a bit. Obviously I will need some sort of for loop etc but im just not sure how to do it. Is it something along the lines of:
What my understanding of what the above is doing is checking how many properties there are an looping round for each property that exists, so all I need to do is get the property 1 at a time however I am not sure where to get the property value to input into the System.getProperty(); method. Any help would be appreciated.
System.getProperties returns a Properties instance which has a propertyNames method which returns an Enumeration instance. You need to loop through the Enumeration - the Javadoc includes an example of how to do this.
Thanks for the responses. I am still having a little trouble with this though. I have tried:
but this just puts it in an endless loop. Im obviously just not getting it. Can anyone direct me further please?
Oh an sorry Bear Bibeault, I was not aware my name breached the rules and I didnt notice the first message, thanks for bringing this to my attention.
posted 5 years ago
System.getProperties().propertyNames() creates a new Enumeration, so every time through the loop you create a new Enumeration and get the first element.
Assign the value returned from System.getProperties().propertyNames() to a variable before the loop and then call the hasMoreElements and nextElement methods on that variable.
This code uses the Properties class from the java.util package as a reference to hold the system properties. To create an object with the required information and assign it to this reference we call the static getProperties() method from the System class. Next call the list method on this object and pass in a PrintStream object to print the properties, in this case we use the default PrintStream which will be directed to the System console.
posted 5 years ago
Rico Felix wrote:You can probably try the following:
Does that separate the properties with line breaks ?