如何在java中读取带空格的属性名称 [英] how to read property name with spaces in java

查看:326
本文介绍了如何在java中读取带空格的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码加载属性文件中存在的所有属性名称:

I am trying to load all the property names present in the properties file using the below code:

for(Enumeration<String> en = (Enumeration<String>) prop.propertyNames();en.hasMoreElements();){

    String key = (String)en.nextElement();
    System.out.println("Property name is "+key);
}

但我的属性文件具有以下内容:

But my properties file has the below contents:

username=
password=
Parent file name=
Child file name =

运行代码后,输出为:

username password Parent Child

如果属性名称有空格,则只返回第一个单词..

If the property name has spaces, it is only returning the first word..

任何人都可以告诉我该怎么做?

Can any one please tell me how to do this?

推荐答案

您可以转义属性文件中的空格,但我认为它看起来会很丑陋。

You can escape the spaces in your properties file, but I think it will start to look pretty ugly.

username=a
password=b
Parent\ file\ name=c
Child\ file\ name=d

您最好用 split() 的indexOf() 或者你的心想要避免任何未来的错误和/或头痛。

You might be better of writing your own implementation with split() or indexOf() or whatever your heart desires to avoid any future bugs and/or headaches.

这篇关于如何在java中读取带空格的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆