加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java属性对象到String

发布时间:2020-12-14 16:35:39 所属栏目:Java 来源:网络整理
导读:我有一个 Java属性对象,我从一个内存中的字符串加载,这是先前从实际的.properties文件加载到内存,如下所示: this.propertyFilesCache.put(file,FileUtils.fileToString(propFile)); util fileToString实际上从文件中读取文本,其余的代码将其存储在名为prope
我有一个 Java属性对象,我从一个内存中的字符串加载,这是先前从实际的.properties文件加载到内存,如下所示:
this.propertyFilesCache.put(file,FileUtils.fileToString(propFile));

util fileToString实际上从文件中读取文本,其余的代码将其存储在名为propertyFilesCache的HashMap中.之后,我将文本文本从HashMap中读取为String,并将其重新加载到Java Properties对象中,如下所示:

String propFileStr = this.propertyFilesCache.get(fileName);
Properties tempProps = new Properties();
try {
    tempProps.load(new ByteArrayInputStream(propFileStr.getBytes()));
} catch (Exception e) {
    log.debug(e.getMessage());
}
tempProps.setProperty(prop,propVal);

在这一点上,我已经在我的内存属性文件中替换了我的属性,我想从Properties对象获取文本,就像我正在读取像上面提到的File对象一样.有没有一个简单的方法来做到这一点,或者我将不得不迭代属性并手动创建String?

解决方法

public static String getPropertyAsString(Properties prop) {    
  StringWriter writer = new StringWriter();
  prop.list(new PrintWriter(writer));
  return writer.getBuffer().toString();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读