【问题标题】:Parsing string as properties将字符串解析为属性
【发布时间】:2012-03-29 03:54:36
【问题描述】:

我正在从数据库中读取一个属性文件。 我检查了java.util.Properties 并且没有从String 实例解析的方法。有什么办法吗?

【问题讨论】:

  • 属性文件的格式是什么?
  • 来自数据库的属性文件或属性??

标签: java


【解决方案1】:

你说得对,java.util.Properties 没有从 String 读取的方法 - 但实际上它具有从 InputStreamReader 读取的更通用的方法。

因此,如果您有某种方式可以将您的 String 呈现为其中任何一种,您可以调用 load,即有效地逐个迭代字符的源。感觉它应该存在,而且确实存在 - java.io.StringReader

那么,将它们放在一起非常简单:

public Properties parsePropertiesString(String s) {
    // grr at load() returning void rather than the Properties object
    // so this takes 3 lines instead of "return new Properties().load(...);"
    final Properties p = new Properties();
    p.load(new StringReader(s));
    return p;
}

【讨论】:

  • 谢谢你,andrzej,在尝试使用 jets3t 动态转换和 Amazon S3 道具文件数小时后,这对我有帮助,太棒了,太简单了。
  • 我对“grr”的感觉非常强烈。 Java 核心库和 API 感觉针对最大样板进行了优化。
【解决方案2】:

这两个实用方法可以通过java.util.Properties 对象写入和读取String

/**
 * Converts a {@link Properties} object to {@link String} and you can
 * also provide a description for the output.
 *
 * @param props an input {@link Properties} to be converted to {@link String}
 * @param desc  an input description for the output
 * @return an output String that could easily parse back to {@link Properties} object
 * @throws IOException If writing encounter a problem or if an I/O error occurs.
 */
private static String convert2String(final Properties props, String desc) throws IOException {
    final StringWriter sw = new StringWriter();
    String propStr;
    try {
        props.store(sw, desc);
        propStr = sw.toString();
    } finally {
        if (sw != null) {
            sw.close();
        }
    }

    return propStr;
}


/**
 * Converts {@link String} to {@link Properties}
 * @param propsStr an {@link String} input that is saved via convert2String method
 * @return a {@link Properties} object
 * @throws IOException if an error occurred when reading from the {@link StringReader}
 */
private static Properties convert2Properties(final String propsStr) throws IOException {
    final Properties props = new Properties();
    final StringReader sr = new StringReader(propsStr);
    try {
        props.load(sr);
    } finally {
        if (sr != null)
            sr.close();
    }

    return props;
}

我发现上述两种方法在有大量属性时很有用 并且您想将它们全部保存在storage or database 中,并且您不想创建一个巨大的key-value 存储表。

【讨论】:

    【解决方案3】:

    我使用此代码从单个 DB 列加载属性

    public Properties buildProperties(String propertiesFromString, String entrySeparator) throws IOException {
        Properties properties = new Properties();
        properties.load(new StringReader(propertiesFromString.replaceAll(entrySeparator, "\n")));
        return properties;
    }
    

    通过一个简单的测试

    @Test
    public void testProperties() throws Exception {
        Properties properties = buildProperties("A=1;B=2;Z=x",";");
        assertEquals("1", properties.getProperty("A"));        
        assertEquals("2", properties.getProperty("B"));        
        assertEquals("3", properties.getProperty("C","3"));        
        assertNull(properties.getProperty("Y"));
        assertEquals("x", properties.getProperty("Z"));  
    }
    

    【讨论】:

      【解决方案4】:

      我们遇到了类似的问题,以上方法对我们不起作用。

      然而,下面的却做到了。

      def content = readFile 'gradle.properties'
      
      Properties properties = new Properties()
      InputStream is = new ByteArrayInputStream(content.getBytes());
      properties.load(is)
      
      def runtimeString = 'SERVICE_VERSION_MINOR'
      echo properties."$runtimeString"
      SERVICE_VERSION_MINOR = properties."$runtimeString"
      echo SERVICE_VERSION_MINOR
      

      【讨论】:

      • 这并不能真正回答问题。如果您有其他问题,可以点击 进行提问。一旦你有足够的reputation,你也可以add a bounty 来引起对这个问题的更多关注。 - From Review
      • 以上内容确实回答了将字符串解析为属性的问题。 - 我们获取一个字符串文件 - 解析它 - 在 Properties 对象中设置字符串等于可以在管道中的任何地方重用的 groovy 对象
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 2014-08-18
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      相关资源
      最近更新 更多