Is there a way to save Properties in Java with some formatting using the Properties object? Like is there a way to introduce new lines between entries? Or comments before each key?
I know this can be easilly done with normal I/O but wondered if there's a way of doing it with the properties object.
The key to writing a comment between each set of properties is to store them in multiple Properties objects.
ie
FileOutputStream fos = new FileOutputStream("c:/myconfig.property");
Properties prop = new Properties();
prop.put("com.app.port", "8080");
prop.put("com.app.ip", "127.0.0.1");
prop.store(fos, "A Test to write properties");
fos.flush();
Properties prop2 = new Properties();
prop2.put("com.app.another", "Hello World");
prop2.store(fos, "Where does this go?");
fos.flush();
fos.close();
This will produce an output such as
#A Test to write properties
#Fri Apr 08 15:28:26 ADT 2011
com.app.ip=127.0.0.1
com.app.port=8080
#Where does this go?
#Fri Apr 08 15:28:26 ADT 2011
com.app.another=Hello World
I have made a class that handles comments in properties.
Both general header comments and comments for individual properties.
Have a look at : CommentedProperties JavaDoc
The jar file can be downloaded here : Download jar file from sourceforge
No. How would the Properties element know what comments to write before each key?
You can include file-level comments when you Properties.store( Writer, String ). After that comment and a timestamp comment:
Then every entry in this Properties table is written out, one per line.
For each entry the key string is written, then an ASCII =, then the associated
element string. For the key, all space characters are written with a
preceding \ character. For the element, leading space characters, but not
embedded or trailing space characters, are written with a preceding \ character.
The key and element characters #, !, =, and : are written with a preceding
backslash to ensure that they are properly loaded.
On the other hand, you can provide instructions on writing extra lines and comments in properties files -- using a Properties object as a source of data.
The Properties object itself doesn't retain any details about the structure of how it was saved in the file. It just has a map of data, which in fact means it won't even necessary write them in the same order they were read. You'll have to use normal I/O to keep the formatting and make your desired changes.
The class CommentedProperties
Will parse the properties
## General comment line 1
## General comment line 2
##!General comment line 3, is ignored and not loaded
## General comment line 4
# Property A comment line 1
A=1
# Property B comment line 1
# Property B comment line 2
B=2
! Property C comment line 1
! Property C comment line 2
C=3
D=4
# Property E comment line 1
! Property E comment line 2
E=5
# Property F comment line 1
#!Property F comment line 2, is ignored and not loaded
! Property F comment line 3
F=5
The properties file comments is:
General comment line 1
General comment line 2
General comment line 4
So Property "A" comments is:
Property A comment line 1
So Property "B" comments is:
Property B comment line 1
Property B comment line 2
So Property "C"
Property C comment line 1
Property C comment line 2
So Property "D" comments is empty.
So Property "E" comments is:
Property E comment line 1
Property E comment line 2
So Property "F" comments is:
Property F comment line 1
Property F comment line 3
Related
I am very new to R and am looking for a possible solution for this problem.
Suppose I have a variables.txt file (or any other file for that matter), which contains a list of variable names. EX, Product,
Ingredient,
Label,
Manufacturer,
Marketing,
This text file is generated in java and this file has to be read in R and variable are to be named according to the names in the file.
My example code is :
list(Product=0,Ingredient=0,Label=0,Manufacturer=0,Marketing=0)
which is now manually hard coded.
I need a way to get these names of variables from the variables.txt file and dynamically assign them in R. How can this be done?? is there any config file concept in R so that can also be a way out??
Maybe you can use:
data = read.table("file.txt",header=TRUE, sep=".") ?
The sep is depends on the seperator in the file. It could be comma, tab, space, dot or whatever.
With header=TRUE that means you want to take the original variable name from the file.
If you need the list structure described above you can use any read.table or read.csv command to get the names into R as mthbnd showed above.
Say your file.txt looks like: Product,Ingredient,Label,Manufacturer,Marketing
Read in the file and create a list from it. The Elements will then be filled with logical(0). Then you can easily set all elements to a 0 by using [ ] in order to keep the list structure
vars <- as.list(read.csv(file = "file.txt", header = T))
vars[] <- 0
My requirement was to update a key/value pair property file for which Commons Configuration is used.
But problem is when you save any text using this api it remove space after comma in a value.
If you disable parsing then it create multiple keys of safe name broken by comma :(
PropertiesConfiguration config = new PropertiesConfiguration("prop.properties");
//config.setDelimiterParsingDisabled(true);
config.save();
Expected value (with no truncation of spaces after before comma):
Name = some , Text , for testing
If setDelimiterParsingDisabled is false then below is outout all spaces gone
Name = some,Text,for testing
If that is True then below is output
Name = some
Name = Text
Name = for testing
I need first one with all space intact means key as is...how to do that
I believe both things cannot be achieved so what is id is I changed Delimeter to carrot ^ sign and not it behaving as I like.
So this is answered.
I have a file which contains properties like :
MyKey=value1:value2
I am using Properties.load to load these into a property object and then outputting the values into another file (using Property.store ).
But the new file is delimiting it with \
MyKey=value1\:value2
Why is this happening ?
This happens, because : is like = a reserved char.
Truth = Beauty
Truth:Beauty
Truth :Beauty
All these lines will set the value for the Property with the Key Truth to Beauty
http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.Reader)
The write method will escape the : sign to \:. After loading this chars will be removed.
I'd like to know how to write comments in properties file that is used like this
PF47=Ankomstdatum måste anges innan SIS kan avslutas. # AF PCT
# is supposed to mean a comment but when I render the messages with fmt format JSTL tag output includes the comment. Do comments in properties file have to be on a separate line?
Thank you
It seems as if java-property files only allow comments on separate lines, so you need to write
# AF PCT
PF47=Ankomstdatum måste anges innan SIS kan avslutas.
The format is described in the java.util.Properties documentation. And yes, the # character is only considered as the start of a comment if it's the first non-whitespace character of the line.
I have a value in a Java .properties file that needs to end in a backlash. The property value should be "\\server\folder\", and I enter the value like so:
name=\\\\server\\folder\\
The trailing backslash is killing whatever property comes on the next line. Am I escaping this incorrectly?
Not sure what the problem is in your case, but this snippet
Properties props = new Properties();
props.load(new FileInputStream("filename.txt"));
System.out.println(props);
Prints
{prop3=val3, prop2=val2\, prop1=val1}
If filename.txt contains
prop1=val1
prop2=val2\\
prop3=val3
Note that a single (or actually, an odd number) of \ in the end of a property line would escape the newline character and things gets messed up.