AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Pdfkey pro java configuration9/19/2023 ![]() ![]() ![]() Then, if you later change your mind, you can supply a different class that implements the same interface. If you're unsure how you want to handle configuration, do it in a class that implements an interface you define. You'll need to use a method of externalizing configuration that is compatible with the container platform - simply reading and writing files is often inappropriate in such an environment. If you ever intend to run the application in a container (Docker, etc), then completely different considerations apply. Unfortunately, this approach does not allow for easy editing of the configuration outside the program. It takes a bit of work to structure a program to be serialized, but it's worth it in the long run. This allows configuration of arbitrary complexity without additional coding, since you're storing Java data directly. In practice, I nearly always externalize configuration in serialized Java objects, into a file whose location I control. Using a text-based format allows users to edit the configuration using other tools, if they so wish. All these file formats are well-supported by Java. If your configuration is more complex than 'name=value', you could read/write it in a format of your choice - JSON, YAML, XML, etc. This method does allow a specific file location but, of course, controlling the file location does require some adaptation for the various platforms you might support. For simple 'name=value' configuration, you can use a Properties object, which has methods for handling files. File appConfig new File ('afile') FileInputStream propsInput new. ![]() Rather than use the preferences API you can read/write files explicitly. I think you can use to solve your problem to write to your properties first create a new Properties object, load them via your InputStream, use methods such as setProperty to add to your configuration and finally use store to write to them. Using this API is convenient, but doesn't allow complex data to be stored, and doesn't easily allow the programmer to provide the application with a way to read configuration from some specific place. And so the language has first class support for properties the a utility class designed for handling this type of configuration files. On Linux, the data is actually stored in an XML file in a subdirectory of $HOME/.java but, of course, the programmer isn't supposed to be concerned about this. Most Java application need to use properties at some point, generally to store simple parameters as key-value pairs, outside of compiled code. The Java preferences API is an attempt to get around this problem by providing a platform-neutral API for preferences. There's no straightforward answer to the question how to externalize configuration - it depends on the application and the environment in which it will be deployed. ![]()
0 Comments
Read More
Leave a Reply. |