I am working on some automation stuff, in which I just want to write the values into the application.properties file as shown below:
project.ids = 01234, 56789, 14587,...
So here, my key(project.ids) will be the same, I just want to append the ids to the previously stored values (shown above). Whenever my service will be called a new project id will be generated so I just want to append or store the value in the property file to the same key(project.ids).
Could anyone help or suggest here how can I achieve this?
Any suggestions around how to write the value to the application.properties file in Java?
I would suggest to use the Environment class of Spring. But it is only used to read properties and not write them.
what you could do is follow this way:
Load the .properties file:
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("application.properties");
prop.load(in);
Get the value as it is of the property
String ids = prop.getProperty("project.ids");
Set the new value
String ids = prop.setProperty("project.ids", ids + ", 12324...");
PS: I wouldn't play around with the .properties file much, it is mostly used for configurations. Try saving your ids in a DB, Spring can be very helpful for that.
Related
in my game i'm loading properties files from a specific level folder with this code:
Properties prop = new Properties
public void LoadLevel(int levelID) {
try {
prop.load(new FileInputStream("/resources/levels" + levelID + "lev.properties"));
....snip....
if I want to load another level, how do I unload the last level's properties file?
I found a solution, but it was blurred as the site was pay only, so I came crawling back here :D
Properties is a Hashtable. You can clear it.
Another alternative is to just create a new Properties object, load it, and assign it to prop. This should work just fine, unless your code has saved the reference to the original object in other places.
How could I search for string in text file in java?
Would it have to be in a text file or could it read a .ini or some other file type
example
sound = off
opengl = on
openal = off
I tried searching for it on google but I couldnt really find anything.
Thanks in advance
It looks like you want to use a Properties file.
You can load one using Properties.load(String path);
If your file is a properties file or an ini file (key = value), you may want to use the Apache Commons Configuration API. It is much better than the Properties class from the JDK.
There are tons of information with those typical of questions.
Here you have two easy examples:
Loading Java Properties Files http://viralpatel.net/blogs/2009/10/loading-java-properties-files.html
How do I load property settings with the Properties class? http://www.jguru.com/faq/view.jsp?EID=581608
In short it is easy to load a file into a Properties object, for example to obtain, in your case, the sound value in a example.properties file:
Properties props = new Properties();
props.load(new FileInputStream("example.properties"));
String isOnOrOff = props.getProperty("sound");
For a tool development, I wish to have basic information such as InputPath, OutputPath, DB server name (username, pwd and DBname) etc configured before the user uses the tool.
Can it be easier to have this information in XML file and read this file from java code? or create an *.properties file and use it.
I am using netbeans 7.0 version.
The idea is to have default information will be used from the file, if the user didnt modify the default info.
Also, the xml which used to store the default info shall be available to the user for updating it.
Thank you,
Ramm
Either approach is quite acceptable.
The Properties class in the standard libraries will allow you to use the properties format or xml format with very few code changes.
The .properties approach
Properties props = new Properties();
props.load(new FileInputStream("user.properties"));
//get a value with a default of NOTDEFINED if there is no value found.
String dbPath = props.getProperty("databasePath","NOTDEFINED");
The .xml approach
Properties props = new Properties();
props.loadFromXML(new FileInputStream("user.properties"));
String dbPath = props.getProperty("databasePath","NOTDEFINED");
Any default properties file which java can automatcially load?
The short answer is no.
The somewhat longer answer starts with a question itself: What should be configured by this file?
For the logging-API of java (java.util.logging) exists a standard-properties-file to configure it. Other frameworks may as well use standard-configuration files. But that always configure only stuff specific for this framework.
If you want to have persistent configuration, you probably want to use the Preference-API. That allows you to save configuration-data, that stays with the user or the JVM.
You can use your own default properties file. You can do it with just few lines.
There is also built in properties, however, these are no simpler, but they are standard.
IMHO Most of the time a plain Proeprties file is used.
This example from the tutorial. This is a more complex example, you can have just one properties file.
// create and load default properties
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("defaultProperties");
defaultProps.load(in);
in.close();
// create application properties with default
Properties applicationProps = new Properties(defaultProps);
// now load properties from last invocation
in = new FileInputStream("appProperties");
applicationProps.load(in);
in.close();
You have to load your properties file yourself or pass arguments at command line.
There is a small library that you can use which will load properties automatically for you: Confucius.
I am trying to make an application launcher that has a settings file that will save 'names' for programs and the path to that program, and when you type the name in an input box it will run the program that name is assigned to.
Also if it the name entered is not known by the application (in the settings file) it will ask the user to add the path and will save that name with the user set path in the settings file.
What I need to know is the best way for me to do this and read/write the file, and the easiest way to organize the settings file to be interpreted.
Any suggestions?
You could use java.util.Properties - it stores key/value pairs in a textfile, and is fairly easy to instantiate. e.g:
Properties mySettings = new Properties();
mySettings.load(new FileInputStream("myapp.cfg"));
// getProperty() returns a String
filepath1 = mySettings.getProperty("filePath1");
Then you simply save your settings in myapp.cfg, either directly (it's a simple textfile with key=value pairs), or via mySettings.store(...). The contents of myapp.cfg will look something like this:
# comment and date added by the Properties object
filePath1=/usr/bin/share/filename
otherVar=52