Module java.base
Package java.util

Class Properties

所有已实现的接口:
Serializable, Cloneable, Map<Object,Object>
直接已知的子类:
Provider

public class Properties extends Hashtable<Object,Object>
Properties类表示一组持久属性。 Properties可以保存到流中或从流中加载。属性列表中的每个键及其对应的值都是字符串。

属性列表可以包含另一个属性列表作为其“默认值”; 如果在原始属性列表中找不到属性键,则会搜索第二个属性列表。

由于Properties继承自Hashtable,因此putputAll方法可以应用于Properties对象。强烈不建议使用它们,因为它们允许调用者插入其键或值不是Strings的条目。应该使用setProperty方法。如果在“受损”的Properties对象上调用storesave方法,其中包含非String键或值,则调用将失败。类似地,如果在包含非String键的“受损”的Properties对象上调用propertyNameslist方法,则调用将失败。

此类的“集合视图”(即entrySet()keySet()values())的iterator方法返回的迭代器可能不是快速失败的(不像Hashtable实现)。这些迭代器保证仅在构造时遍历元素一次,并且可能(但不保证)反映构造后的任何修改。

load(Reader) / store(Writer, String)方法从字符流中以简单的面向行的格式加载和存储属性。 load(InputStream) / store(OutputStream, String)方法与load(Reader)/store(Writer, String)对相同,只是输入/输出流以ISO 8859-1字符编码进行编码。无法直接表示该编码的字符可以使用Unicode转义进行写入,如《Java语言规范》第3.3节中定义的那样;在转义序列中仅允许一个'u'字符。

loadFromXML(InputStream)storeToXML(OutputStream, String, String)方法以简单的XML格式加载和存储属性。默认情况下使用UTF-8字符编码,但如果需要,可以指定特定编码。实现必须支持UTF-8和UTF-16,可能支持其他编码。XML属性文档具有以下DOCTYPE声明:

 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
 
请注意,当导出或导入属性时,不会访问系统URI(http://java.sun.com/dtd/properties.dtd);它仅用作唯一标识DTD的字符串,即:
    <?xml version="1.0" encoding="UTF-8"?>

    <!-- 属性的DTD -->

    <!ELEMENT properties ( comment?, entry* ) >

    <!ATTLIST properties version CDATA #FIXED "1.0">

    <!ELEMENT comment (#PCDATA) >

    <!ELEMENT entry (#PCDATA) >

    <!ATTLIST entry key CDATA #REQUIRED>
 

此类是线程安全的:多个线程可以共享单个Properties对象,无需外部同步。

API注释:
Properties类未从其超类Hashtable继承负载因子的概念。
自:
1.0
参见:
  • Field Details

    • defaults

      protected volatile Properties defaults
      包含在此属性列表中未找到的任何键的默认值的属性列表。
  • Constructor Details

    • Properties

      public Properties()
      创建一个空的属性列表,没有默认值。
      实现注释:
      使用此构造函数创建的Properties对象的初始容量未指定。
    • Properties

      public Properties(int initialCapacity)
      创建一个空的属性列表,没有默认值,并且具有初始大小,可容纳指定数量的元素,无需动态调整大小。
      参数:
      initialCapacity - Properties将被调整大小以容纳这么多元素
      抛出:
      IllegalArgumentException - 如果初始容量小于零。
    • Properties

      public Properties(Properties defaults)
      创建一个具有指定默认值的空属性列表。
      实现注释:
      使用此构造函数创建的Properties对象的初始容量未指定。
      参数:
      defaults - 默认值。
  • Method Details

    • setProperty

      public Object setProperty(String key, String value)
      调用Hashtable方法put。为了与getProperty方法并行,强制使用字符串作为属性键和值。返回的值是Hashtable调用put的结果。
      参数:
      key - 要放入此属性列表中的键。
      value - 与key对应的值。
      返回:
      此属性列表中指定键的先前值,如果没有,则为null
      自:
      1.2
      参见:
    • load

      public void load(Reader reader) throws IOException
      从输入字符流中以简单的面向行的格式读取属性列表(键和元素对)。

      属性按行处理。有两种类型的行,自然行逻辑行。自然行被定义为由一组行终止字符(\n\r\r\n)或流的结尾终止的字符行。自然行可以是空行、注释行,或者包含部分或全部键-元素对的行。逻辑行包含键-元素对的所有数据,可以通过使用反斜杠字符\转义行终止序列,将其分布在相邻的几个自然行中。请注意,注释行不能以这种方式扩展;每个作为注释的自然行必须有自己的注释指示符,如下所述。从输入读取行,直到到达流的末尾。

      只包含空白字符的自然行被视为空白并被忽略。注释行的第一个非空白字符是ASCII '#''!';注释行也被忽略,不编码键-元素信息。除了行终止符,此格式将空格字符(' ''\u0020')、制表符('\t''\u0009')和换页符('\f''\u000C')视为空白。

      如果逻辑行跨越多个自然行,那么转义行终止序列、行终止序列以及以下一行开头的任何空格对键或元素值没有影响。关于键和元素解析(加载时)的其余讨论将假定构成键和元素的所有字符出现在去除行继续字符后的单个自然行上。请注意,仅检查行终止序列之前的字符是否转义是不够的;行终止符必须有奇数个连续反斜杠才能转义。由于输入是从左到右处理的,因此在行终止符(或其他位置)之前具有非零偶数个2n连续反斜杠编码为转义处理后的n个反斜杠。

      键包含从第一个非空白字符开始直到但不包括第一个未转义的'='':'或除行终止符之外的空白字符的行中的所有字符。所有这些键终止字符都可以通过在前面加上反斜杠字符进行转义;例如,

      \:\=

      将是两个字符键":="。行终止字符可以使用\r\n转义序列包含。键后的任何空格都将被跳过;如果键后的第一个非空格字符是'='':',则将被忽略,并且其后的任何空格字符也将被跳过。行中的所有剩余字符都成为关联元素字符串的一部分;如果没有剩余字符,则元素为空字符串""。一旦确定了构成键和元素的原始字符序列,将执行如上所述的转义处理。

      例如,以下三行中的每一行都指定了键"Truth"和关联的元素值"Beauty"

       Truth = Beauty
        Truth:Beauty
       Truth                    :Beauty
       
      另一个例子,以下三行指定了一个属性:
       fruits                           apple, banana, pear, \
                                        cantaloupe, watermelon, \
                                        kiwi, mango
       
      键是"fruits",关联的元素是:
      "apple, banana, pear, cantaloupe, watermelon, kiwi, mango"
      请注意,在每个\之前都有一个空格,以便在最终结果中每个逗号后都会出现一个空格;\、行终止符和续行中的前导空格仅被丢弃,而不是被一个或多个其他字符替换。

      作为第三个例子,行:

      cheeses
       
      指定键为"cheeses",关联的元素为空字符串""

      键和元素中的字符可以用类似于用于字符和字符串文字的转义序列来表示(请参见Java语言规范的第3.3节和3.10.6节)。与用于字符和字符串的字符转义序列和Unicode转义的区别是:

      • 不识别八进制转义。
      • 字符序列\b不表示退格字符。
      • 该方法不将在非有效转义字符之前的反斜杠字符\视为错误;反斜杠会被静默丢弃。例如,在Java字符串中,序列"\z"会导致编译时错误。相比之下,此方法会静默丢弃反斜杠。因此,此方法将两个字符序列"\b"视为等同于单个字符'b'
      • 对于单引号和双引号,不需要转义;但是,根据上述规则,由反斜杠前导的单引号和双引号字符仍会产生单引号和双引号字符。
      • 在Unicode转义序列中只允许一个'u'字符。

      此方法返回后,指定的流保持打开状态。

      参数:
      reader - 输入字符流。
      抛出:
      IOException - 如果从输入流读取时发生错误。
      IllegalArgumentException - 如果输入中出现了格式错误的Unicode转义。
      NullPointerException - 如果reader为null。
      自1.6起:
      1.6
    • load

      public void load(InputStream inStream) throws IOException
      从输入字节流中读取属性列表(键和元素对)。输入流采用简单的面向行的格式,如load(Reader)中所指定的,并假定使用ISO 8859-1字符编码;即每个字节都是一个Latin1字符。不在Latin1中的字符和某些特殊字符使用Java语言规范3.3节中定义的Unicode转义在键和元素中表示。

      此方法返回后,指定的流保持打开状态。

      参数:
      inStream - 输入流。
      抛出:
      IOException - 如果从输入流读取时发生错误。
      IllegalArgumentException - 如果输入流包含格式错误的Unicode转义序列。
      NullPointerException - 如果inStream为null。
      自1.2起:
      1.2
    • save

      @Deprecated public void save(OutputStream out, String comments)
      Deprecated.
      This method does not throw an IOException if an I/O error occurs while saving the property list. The preferred way to save a properties list is via the store(OutputStream out, String comments) method or the storeToXML(OutputStream os, String comment) method.
      调用store(OutputStream out, String comments)方法并抑制抛出的IOException。
      参数:
      out - 输出流。
      comments - 属性列表的描述。
      抛出:
      ClassCastException - 如果此Properties对象包含任何不是Strings的键或值。
    • store

      public void store(Writer writer, String comments) throws IOException
      将此Properties表中的属性列表(键和元素对)以适合使用load(Reader)方法的格式写入输出字符流。

      此方法不会写出此Properties表的默认表中的属性。

      如果comments参数不为null,则首先将ASCII #字符、注释字符串和行分隔符写入输出流。因此,comments可以作为标识性注释。在comments中的换行符(\n)、回车符(\r)或回车符后紧接着的换行符(\r\n)将被替换为line separator,如果comments中的下一个字符不是字符#或字符!,则在该行分隔符之后写入ASCII #

      如果在命令行上设置了java.properties.date且不为空(由String.isEmpty确定),则将写入注释行如下。首先写入一个#字符,然后是属性的内容,然后是行分隔符。系统属性值中的任何行终止符字符都会像上面对comments参数所述的方式一样处理。如果系统属性未设置或为空,则将写入注释行如下。首先写入一个#字符,然后是当前日期和时间格式,格式如同Date.toString方法,然后是行分隔符。

      然后,将此Properties表中的每个条目写出,每行一个。对于每个条目,将写入键字符串,然后是ASCII =,然后是关联的元素字符串。对于键,所有空格字符都将用前导\字符写入。对于元素,前导空格字符,但不是嵌入或尾随空格字符,都将用前导\字符写入。键和元素字符#!=:都将用前导反斜杠写入,以确保它们被正确加载。

      写入条目后,刷新输出流。此方法返回后,输出流保持打开状态。

      Implementation Requirements:
      The keys and elements are written in the natural sort order of the keys in the entrySet() unless entrySet() is overridden by a subclass to return a different value than super.entrySet().
      Parameters:
      writer - an output character stream writer.
      comments - a description of the property list.
      Throws:
      IOException - if writing this property list to the specified output stream throws an IOException.
      ClassCastException - if this Properties object contains any keys or values that are not Strings.
      NullPointerException - if writer is null.
      Since:
      1.6
    • store

      public void store(OutputStream out, String comments) throws IOException
      Writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method.

      Properties from the defaults table of this Properties table (if any) are not written out by this method.

      This method outputs the comments, properties keys and values in the same format as specified in store(Writer), with the following differences:

      • The stream is written using the ISO 8859-1 character encoding.
      • Characters not in Latin-1 in the comments are written as \uxxxx for their appropriate unicode hexadecimal value xxxx.
      • Characters less than \u0020 and characters greater than \u007E in property keys or values are written as \uxxxx for the appropriate hexadecimal value xxxx.

      After the entries have been written, the output stream is flushed. The output stream remains open after this method returns.

      Parameters:
      out - an output stream.
      comments - a description of the property list.
      Throws:
      IOException - if writing this property list to the specified output stream throws an IOException.
      ClassCastException - if this Properties object contains any keys or values that are not Strings.
      NullPointerException - if out is null.
      Since:
      1.2
    • loadFromXML

      public void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException
      Loads all of the properties represented by the XML document on the specified input stream into this properties table.

      The XML document must have the following DOCTYPE declaration:

       <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
       
      Furthermore, the document must satisfy the properties DTD described above.

      An implementation is required to read XML documents that use the "UTF-8" or "UTF-16" encoding. An implementation may support additional encodings.

      The specified stream is closed after this method returns.

      Parameters:
      in - the input stream from which to read the XML document.
      Throws:
      IOException - if reading from the specified input stream results in an IOException.
      UnsupportedEncodingException - if the document's encoding declaration can be read and it specifies an encoding that is not supported
      InvalidPropertiesFormatException - Data on input stream does not constitute a valid XML document with the mandated document type.
      NullPointerException - if in is null.
      Since:
      1.5
      External Specifications
      See Also:
    • storeToXML

      public void storeToXML(OutputStream os, String comment) throws IOException
      Emits an XML document representing all of the properties contained in this table.

      An invocation of this method of the form props.storeToXML(os, comment) behaves in exactly the same way as the invocation props.storeToXML(os, comment, "UTF-8");.

      Parameters:
      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.
      Throws:
      IOException - if writing to the specified output stream results in an IOException.
      NullPointerException - if os is null.
      ClassCastException - if this Properties object contains any keys or values that are not Strings.
      Since:
      1.5
      See Also:
    • storeToXML

      public void storeToXML(OutputStream os, String comment, String encoding) throws IOException
      Emits an XML document representing all of the properties contained in this table, using the specified encoding.

      The XML document will have the following DOCTYPE declaration:

       <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
       

      If the specified comment is null then no comment will be stored in the document.

      An implementation is required to support writing of XML documents that use the "UTF-8" or "UTF-16" encoding. An implementation may support additional encodings.

      The specified stream remains open after this method returns.

      This method behaves the same as storeToXML(OutputStream os, String comment, Charset charset) except that it will look up the charset using the given encoding name.

      Parameters:
      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 encoding
      Throws:
      IOException - if writing to the specified output stream results in an IOException.
      UnsupportedEncodingException - if the encoding is not supported by the implementation.
      NullPointerException - if os is null, or if encoding is null.
      ClassCastException - if this Properties object contains any keys or values that are not Strings.
      Since:
      1.5
      External Specifications
      See Also:
    • storeToXML

      public void storeToXML(OutputStream os, String comment, Charset charset) throws IOException
      Emits an XML document representing all of the properties contained in this table, using the specified encoding.

      The XML document will have the following DOCTYPE declaration:

       <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
       

      If the specified comment is null then no comment will be stored in the document.

      An implementation is required to support writing of XML documents that use the "UTF-8" or "UTF-16" encoding. An implementation may support additional encodings.

      Unmappable characters for the specified charset will be encoded as numeric character references.

      The specified stream remains open after this method returns.

      Parameters:
      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.
      charset - the charset
      Throws:
      IOException - if writing to the specified output stream results in an IOException.
      NullPointerException - if os or charset is null.
      ClassCastException - if this Properties object contains any keys or values that are not Strings.
      Since:
      10
      External Specifications
      See Also:
    • getProperty

      public String getProperty(String key)
      在此属性列表中搜索具有指定键的属性。如果在此属性列表中找不到键,则会递归检查默认属性列表及其默认值。如果未找到属性,则该方法返回null
      参数:
      key - 属性键。
      返回:
      具有指定键值的属性列表中的值。
      参见:
    • getProperty

      public String getProperty(String key, String defaultValue)
      在此属性列表中搜索具有指定键的属性。如果在此属性列表中找不到键,则会递归检查默认属性列表及其默认值。如果未找到属性,则该方法返回默认值参数。
      参数:
      key - 哈希表键。
      defaultValue - 默认值。
      返回:
      具有指定键值的属性列表中的值。
      参见:
    • propertyNames

      public Enumeration<?> propertyNames()
      返回此属性列表中所有键的枚举,包括默认属性列表中的不同键(如果尚未从主属性列表中找到同名键)。
      返回:
      此属性列表中所有键的枚举,包括默认属性列表中的键。
      抛出:
      ClassCastException - 如果此属性列表中的任何键不是字符串。
      参见:
    • stringPropertyNames

      public Set<String> stringPropertyNames()
      返回此属性列表中键和相应值均为字符串的不可修改键集,包括默认属性列表中的不同键(如果尚未从主属性列表中找到同名键)。省略键或值不是String 类型的属性。

      返回的集合不是由此Properties 对象支持的。对此Properties 对象的更改不会反映在返回的集合中。

      返回:
      此属性列表中键和相应值均为字符串的不可修改键集,包括默认属性列表中的键。
      自:
      1.6
      参见:
    • list

      public void list(PrintStream out)
      将此属性列表打印到指定的输出流。此方法对调试很有用。
      参数:
      out - 输出流。
      抛出:
      ClassCastException - 如果此属性列表中的任何键不是字符串。
    • list

      public void list(PrintWriter out)
      将此属性列表打印到指定的输出流。此方法对调试很有用。
      参数:
      out - 输出流。
      抛出:
      ClassCastException - 如果此属性列表中的任何键不是字符串。
      自:
      1.1