Reputation: 43
I am trying to tackle a program take the given input file is in.properties
and I want to write it out again to a new file out.properties
discarding the prefix prefix
from the file contents
i.e. the contents of the input file would be
prefix.sum.code.root=/compile/pkg
the contents of the output file would be
sum.code.root=/compile/pkg
Here is my Code :
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Dummy {
public static void main(String args[])
{
Properties prop =new Properties();
try{
//load a property file
prop.load(new FileInputStream ("C:\\Users\\user\\Desktop\\ant\\Filtering\\input.properties"));
for (String key : prop.stringPropertyNames()){
prop.remove(key);
}
prop.store(new FileOutputStream("C:\\Users\\user\\Desktop\\ant\\Filtering\\output.properties"), null);
}catch (IOException e)
{
e.printStackTrace();
}
}}
The returning null value for the 'KEY' and i am not able to update new value into this feild
Upvotes: 2
Views: 2948
Reputation: 29646
You should be able to do this fairly easy using Properties.stringPropertyNames
and Hashtable.remove
, as Properties
is a subclass of Hashtable
.
final Properties properties = ...;
for (final String name : properties.stringPropertyNames()) {
if (name.startsWith("prefix.")) {
properties.setProperty(name.substring(6, name.length()), properties.remove(name));
}
}
Upvotes: 1