读取 Java 属性文件而不转义值
我的应用程序需要使用 .properties 文件进行配置。 在属性文件中,用户可以指定路径。
问题
属性文件需要对值进行转义,例如
dir = c:\\mydir
需要
我需要某种方式来接受值未转义的属性文件,以便用户可以指定:
dir = c:\mydir
My application needs to use a .properties file for configuration.
In the properties files, users are allow to specify paths.
Problem
Properties files need values to be escaped, eg
dir = c:\\mydir
Needed
I need some way to accept a properties file where the values are not escaped, so that the users can specify:
dir = c:\mydir
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以尝试使用 guava 的
Splitter
:根据'='
进行拆分,并根据生成的Iterable
构建映射。该方案的缺点是不支持注释。
You could try using guava's
Splitter
: split on'='
and build a map from resultingIterable
.The disadvantage of this solution is that it does not support comments.
@pdeva:还有一种解决方案
@pdeva: one more solution
这不是您问题的确切答案,而是可能适合您需求的不同解决方案。在 Java 中,您可以使用
/
作为路径分隔符,它适用于 Windows、Linux 和 OSX。这对于相对路径特别有用。在您的示例中,您可以使用:
It's not an exact answer to your question, but a different solution that may be appropriate to your needs. In Java, you can use
/
as a path separator and it'll work on both Windows, Linux, and OSX. This is specially useful for relative paths.In your example, you could use:
为什么不简单地扩展属性类以合并双正斜杠的剥离。这样做的一个很好的功能是,在程序的其余部分中,您仍然可以使用原始的
Properties
类。使用新类很简单:
剥离代码也可以改进,但一般原则就在那里。
Why not simply extend the properties class to incorporate stripping of double forward slashes. A good feature of this will be that through the rest of your program you can still use the original
Properties
class.Using the new class is a simple as:
The stripping code could also be improved upon but the general principle is there.
两个选项:
Two options:
您可以在加载属性之前“预处理”文件,例如:
您的代码可能看起来像这样 这样
做,您的 .properties 文件将看起来像您所需要的,但您将拥有可供使用的属性值。
*我知道应该有更好的方法来操作文件,但我希望这会有所帮助。
You can "preprocess" the file before loading the properties, for example:
And your code could look this way
Doing this, your .properties file would look like you need, but you will have the properties values ready to use.
*I know there should be better ways to manipulate files, but I hope this helps.
正确的方法是为您的用户提供一个属性文件编辑器(或他们最喜欢的文本编辑器的插件),允许他们以纯文本形式输入文本,并将文件保存为属性文件格式。
如果您不希望这样做,那么您实际上是在为与属性文件相同(或其子集)的内容模型定义新格式。
全程指定您的格式,然后考虑一种方法,将
仅当您确实可以控制属性对象的创建时,这两种方法才能直接起作用,否则您将必须使用应用程序存储转换后的格式。
那么,让我们看看如何定义它。 普通属性文件的内容模型很简单:
您想要避免的转义只是允许任意 Java 字符串,而不仅仅是这些字符串的子集。
通常足够的子集是:
:
或=
)到字符串值(不包含任何前导或尾随空格或行)的映射休息)。在您的示例
dir = c:\mydir
中,键为dir
,值为c:\mydir
。如果我们希望我们的键和值包含任何 Unicode 字符(除了提到的禁止字符),我们应该使用 UTF-8(或 UTF-16)作为存储编码 - 因为我们无法在存储之外转义字符编码。否则,US-ASCII 或 ISO-8859-1(作为普通属性文件)或 Java 支持的任何其他编码就足够了,但请确保将其包含在内容模型的规范中(并确保以这种方式阅读) )。
由于我们限制了内容模型,以便排除所有“危险”字符,因此我们现在可以将文件格式简单地定义为:
键或值中出现的每个
\
现在都是一个 真正的反斜杠,而不是任何逃避其他东西的东西。因此,为了将其转换为原始格式,我们只需将其加倍,就像 Grekz 提出的那样,例如在过滤阅读器中:
然后我们将此 Reader 传递给我们的 Properties 对象(或将内容保存到新文件中)。
相反,我们可以简单地自己解析这种格式。
再次,在此之后使用Properties.store(),我们可以将其以原始格式导出。
The right way would be to provide your users with a property file editor (or a plugin for their favorite text editor) which allows them entering the text as pure text, and would save the file in the property file format.
If you don't want this, you are effectively defining a new format for the same (or a subset of the) content model as the property files have.
Go the whole way and actually specify your format, and then think about a way to either
Properties
object from it.Both of these approaches will only work directly if you actually can control your property object's creation, otherwise you will have to store the transformed format with your application.
So, let's see how we can define this. The content model of normal property files is simple:
The escaping which you want to avoid serves just to allow arbitrary Java strings, and not just a subset of these.
An often sufficient subset would be:
:
or=
) to string values (not containing any leading or trailing white space or line breaks).In your example
dir = c:\mydir
, the key would bedir
and the valuec:\mydir
.If we want our keys and values to contain any Unicode character (other than the forbidden ones mentioned), we should use UTF-8 (or UTF-16) as the storage encoding - since we have no way to escape characters outside of the storage encoding. Otherwise, US-ASCII or ISO-8859-1 (as normal property files) or any other encoding supported by Java would be enough, but make sure to include this in your specification of the content model (and make sure to read it this way).
Since we restricted our content model so that all "dangerous" characters are out of the way, we can now define the file format simply as this:
Every
\
occurring in either key or value now is a real backslash, not anything which escapes something else.Thus, for transforming it into the original format, we simply need to double it, like Grekz proposed, for example in a filtering reader:
Then we would pass this Reader to our Properties object (or save the contents to a new file).
Instead, we could simply parse this format ourselves.
Again, using
Properties.store()
after this, we can export it in the original format.基于@Ian Harrigan,这里是一个完整的解决方案,可以从 ascii 文本文件获取 Netbeans 属性文件(和其他转义属性文件):
Based on @Ian Harrigan, here is a complete solution to get Netbeans properties file (and other escaping properties file) right from and to ascii text-files :