Java property file




















Active 1 year, 7 months ago. Viewed k times. Questions: Do I need to store the file in the same package as the class which will load them, or is there any specific location where it should be placed? Does the file need to end in any specific extension or is. How can I load the file in the code And how can I iterate through the values inside? Honza Zidek 7, 4 4 gold badges 59 59 silver badges 94 94 bronze badges.

Ali Ali k gold badges silver badges bronze badges. Add a comment. Active Oldest Votes. Zed Zed 55k 8 8 gold badges 72 72 silver badges bronze badges. What value is returned when the key is not present in the properties file? MitakshGupta If a property with the name you passed is not found in the file or in the default properties list, it returs null.

See Javadoc — drigoangelo. Are there any problems because you dont close FileInputStream? Any extension is fine, although.

Jon Skeet Jon Skeet 1. Thanks Jon, next thing I know I'll be looking up something on joda and you'll answer that too. If you put the properties file in the same package as class Foo, you can easily load it with new Properties. Fabian Steeg Fabian Steeg You can do this - but I dislike storing properties files in the same package.

You end up with properties files spread all over the place in your application. I'd much rather store all properties files in the root of the app, and load them as "class. Nate, that's true. However, in some scenarios the deployed location is not known e. In such cases it can be quite convenient to say 'it's with that class, wherever that class ends up to be'. Also to avoid spreading the files all over, a single config package can be used for all the property files.

Fabian, both of those cases work with my comment - it's based on the classpath - not the filesystem. For anyone trying to get Nate's example to work- the backslash should be replaced with a forward slash. So in this case: 'class. There are many ways to create and read properties files: Store the file in the same package.

Use theses classes located at java. To read properties, use iterator or enumerator or direct methods of Properties or java. System class. Load new java. FileInputStream "my. Hi AVD, why we need. Alberto Zaccagni Alberto Zaccagni However, some "extensions" of properties files assume the.

We can use only the following types of data: String boolean double float int long byte array To load the the properties file, either you can provide absolute path Or use getResourceAsStream if the properties file is present in your classpath.

Ravindra babu Ravindra babu 45k 8 8 gold badges silver badges bronze badges. Hash 4, 5 5 gold badges 19 19 silver badges 37 37 bronze badges.

In order: You can store the file pretty much anywhere. Montecristo has illustrated how to load this. In that case output of the readPropertyFileFromClasspath method is:.

Also, notice that when we use the same Properties object to load another property file, we should clear its contents using clear method.

If we pass any key for which there is no value stored in the properties object, it returns null. Thank you so much for this. I was into this issue and tired to tinker around to check if its possible but couldnt get it done.

Now that i have seen the way you did it, thanks guys with regards. Your email address will not be published. Prev Java read file line by line. Next Enum in Java. Pankaj I love Open Source technologies and writing about my experience about them is my passion.

Follow Author. Java provides us with a Properties class of the java. Before we proceed to set the properties, we need to get the file properties from the directory where it is located, and if it does not exist, we create a new file. We create a Properties object using new Properties and call the setProperty method to set the property values in the file. The setProperty method takes two arguments, the first is the key, and the second is the value.

In the example, we set three properties and finally store the properties in the file we call the store method to pass the outputStream and an optional comment as arguments. The properties file should be created and have the properties inserted in it.



0コメント

  • 1000 / 1000