public class Properties extends AbstractMap<String,String>
java.util.Properties, with the difference that it
properly supports comments both for reading and writing. It also maintains an exact
representation of the input, meaning that when an input is read and later written out again the
output will match the input exactly. Methods exist for obtaining and setting comments on
properties.AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>| Constructor and Description |
|---|
Properties() |
Properties(Properties defaults) |
| Modifier and Type | Method and Description |
|---|---|
Properties |
asJUProperties()
Returns a
java.util.Properties with the same contents as this object. |
void |
clear() |
Set<Map.Entry<String,String>> |
entrySet() |
Properties |
escaped()
Returns a copy of the object where all characters, in keys and values that are not in the
Unicode range of 0x0000-0x00FF, have been escaped.
|
Properties |
flatten()
Deprecated.
Use
flattened() |
Properties |
flattened()
Returns the current properties table with all its defaults as a single flattened properties
table.
|
String |
get(Object key) |
List<String> |
getComment(String key)
Gather all the comments directly before the given key and return them as a list.
|
String |
getProperty(String key)
Searches for the property with the specified key in this property list.
|
String |
getProperty(String key,
String defaultValue)
Searches for the property with the specified key in this property list.
|
List<String> |
getPropertyComment(String key)
Searches for the property with the specified key in this property list.
|
String |
getRaw(String rawKey)
Works like
get() but returns the raw value associated with the given raw key. |
void |
list(PrintStream out)
Prints this property list out to the specified output stream.
|
void |
list(PrintWriter out)
Prints this property list out to the specified writer.
|
void |
load(InputStream in)
Loads the contents from the input and stores it in this object.
|
void |
load(Path file)
Loads the contents from the given file and stores it in this object.
|
void |
load(Reader reader)
Loads the contents from the reader and stores it in this object.
|
void |
loadFromXML(InputStream in)
Loads all the properties represented by the XML document on the specified input stream into
this properties table.
|
static Properties |
loadProperties(InputStream in)
Returns a
Properties with the contents read from the given stream. |
static Properties |
loadProperties(Path file)
Returns a
Properties with the contents read from the given file. |
static Properties |
loadProperties(Reader reader)
Returns a
Properties with the contents read from the given reader. |
Enumeration<String> |
propertyNames()
Returns an enumeration of keys from this property list where the key and its corresponding
value are strings, including distinct keys in the default property list if a key of the same
name has not already been found from the main properties table.
|
String |
put(String key,
String value) |
String |
putCommented(String key,
String value,
String... comment)
Associates the specified value with the specified key in this properties table.
|
String |
putRaw(String rawKey,
String rawValue)
Works like
put() but uses raw values for keys and values. |
Set<Map.Entry<String,String>> |
rawEntrySet()
Works like
entrySet() but returning the raw values. |
Set<String> |
rawKeySet()
Works like
keySet() but returning the keys' raw values. |
Collection<String> |
rawValues()
Works like
values() but returning the raw values. |
String |
remove(Object key) |
List<String> |
setComment(String key,
List<String> comments)
Adds the list of comments to the item indicated by the given key.
|
List<String> |
setComment(String key,
String... comments)
Adds the given comments to the item indicated by the given key.
|
String |
setProperty(String key,
String value,
String... comment)
Associates the specified value with the specified key in this properties table.
|
void |
store(OutputStream out,
String... comment)
Stores the contents of this object to the given file.
|
void |
store(Path file,
String... comment)
Stores the contents of this object to the given file.
|
void |
store(Writer writer,
String... comment)
Stores the contents of this object to the given file.
|
void |
storeToXML(OutputStream os,
String comment)
Emits an XML document representing all the properties contained in this table.
|
void |
storeToXML(OutputStream os,
String comment,
String encoding)
Emits an XML document representing all the properties contained in this table.
|
Set<String> |
stringPropertyNames()
Returns an unmodifiable set of keys from this property list where the key and its
corresponding value are strings, including distinct keys in the default property list if a
key of the same name has not already been found from the main properties table.
|
Properties |
unescaped()
Returns a copy of the object where all Unicode escape sequences, in keys and values, have
been decoded into their actual Unicode characters.
|
clone, containsKey, containsValue, equals, hashCode, isEmpty, keySet, putAll, size, toString, valuesfinalize, getClass, notify, notifyAll, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAllpublic Properties()
public Properties(Properties defaults)
public String getProperty(String key)
key - the key to look up.null.public String getProperty(String key, String defaultValue)
key - the key to look up.defaultValue - the value to return if no mapping was found for the key.
defaultValue.public List<String> getPropertyComment(String key)
key - the key to look up.public String setProperty(String key, String value, String... comment)
key - key with which the specified value is to be associatedvalue - value to be associated with the specified keycomment - comment lines to be associated with the specified keypublic Enumeration<String> propertyNames()
public Set<String> stringPropertyNames()
public void list(PrintStream out)
out - a PrintStream objectpublic void list(PrintWriter out)
out - a PrintWriter objectpublic void loadFromXML(InputStream in) throws IOException
in - the input stream from which to read the XML document.IOException - Thrown when any IO error occurs during loadingpublic void storeToXML(OutputStream os, String comment) throws IOException
os - the output stream on which to emit the XML document.comment - a description of the property list, or null if no comment is desired.IOExceptionpublic void storeToXML(OutputStream os, String comment, String encoding) throws IOException
os - the output stream on which to emit the XML document.comment - a description of the property list, or null if no comment is desired.encoding - the name of a supported character encodingIOException@Deprecated public Properties flatten()
flattened()Properties objectpublic Properties flattened()
Properties objectpublic Set<String> rawKeySet()
keySet() but returning the keys' raw values. Meaning that the keys
haven't been unescaped before being returned.public Collection<String> rawValues()
values() but returning the raw values. Meaning that the values have
not been unescaped before being returned.public Set<Map.Entry<String,String>> rawEntrySet()
entrySet() but returning the raw values. Meaning that the values have
not been unescaped before being returned.public String getRaw(String rawKey)
get() but returns the raw value associated with the given raw key.
This means that the value won't be unescaped before being returned.rawKey - The key, in raw format, to look upnull if the key wasn't foundpublic String putCommented(String key, String value, String... comment)
key - key with which the specified value is to be associatedvalue - value to be associated with the specified keycomment - comment lines to be associated with the specified keypublic String putRaw(String rawKey, String rawValue)
put() but uses raw values for keys and values. This means these keys
and values will not be escaped before being stored.rawKey - key with which the specified value is to be associatedrawValue - value to be associated with the specified keypublic void clear()
public List<String> getComment(String key)
key - The key to look forpublic List<String> setComment(String key, String... comments)
#
or !, but if none is encountered the code will select one for you (it will look
at any existing comments, or at symbols found on previous items and as a last result will use
# ).key - The key to look forcomments - The comments to add to the itemNoSuchElementException - Thrown when they key couldn't be foundpublic List<String> setComment(String key, List<String> comments)
#
or !, but if none is encountered the code will select one for you (it
will look at any existing comments, or at symbols found on previous items and as a last
result will use # ).key - The key to look forcomments - The list of comments to add to the itemNoSuchElementException - Thrown when they key couldn't be foundpublic Properties escaped()
store()
to write to an output that does not support UTF8.Properties with encoded keys and valuespublic Properties unescaped()
store()
to write to an output that supports UTF8.Properties without Unicode escape sequences in its keys and valuespublic Properties asJUProperties()
java.util.Properties with the same contents as this object. The
information is a copy, changes to one Properties object will not affect the other.java.util.Properties objectpublic void load(Path file) throws IOException
file - a path to the file to loadIOException - Thrown when any IO error occurs during loadingpublic void load(InputStream in) throws IOException
in - an InputStream objectIOException - Thrown when any IO error occurs during loadingpublic void load(Reader reader) throws IOException
reader - a Reader objectIOException - Thrown when any IO error occurs during loadingpublic static Properties loadProperties(Path file) throws IOException
Properties with the contents read from the given file. This includes
not only properties but also all whitespace and any comments that are encountered.file - a path to the file to loadIOException - Thrown when any IO error occurs during loadingpublic static Properties loadProperties(InputStream in) throws IOException
Properties with the contents read from the given stream. This includes
not only properties but also all whitespace and any comments that are encountered.in - an InputStream objectIOException - Thrown when any IO error occurs during loadingpublic static Properties loadProperties(Reader reader) throws IOException
Properties with the contents read from the given reader. This includes
not only properties but also all whitespace and any comments that are encountered.reader - a Reader objectIOException - Thrown when any IO error occurs during loadingpublic void store(Path file, String... comment) throws IOException
file - a path to the file to writecomment - comment lines to be written at the start of the outputIOException - Thrown when any IO error occurs during operationpublic void store(OutputStream out, String... comment) throws IOException
out - an OutputStream objectcomment - comment lines to be written at the start of the outputIOException - Thrown when any IO error occurs during operationpublic void store(Writer writer, String... comment) throws IOException
writer - a Writer objectcomment - comment lines to be written at the start of the outputIOException - Thrown when any IO error occurs during operationCopyright © 2024. All rights reserved.