java - 在 Java 中,为什么 WindowsPreferences 对大写字母使用斜杠?

标签 java windows registry

我一直在使用 java.util.prefs.Preferences 功能(在 Java 8 中,在 Windows 机器上)。它工作正常,我可以在其中向 Windows 注册表写入新 key 。因此,我使用 Preferences.systemRoot() 获取系统的 Preferences 对象,然后使用 node() 方法获取映射到 Windows 注册表中节点的 Preferences 对象。它正在创造美好的事物。

我为节点使用的 key 是一个全大写字母的字符串(“RBI”)。当我查看 Windows 注册表中的节点时,它显示为“/R/B/I”,名称中带有正斜杠。

我觉得这很奇怪,所以我仔细研究了一下,看起来这是故意的。我找到了在 Windows 环境 (java.util.prefs.WindowsPreferences) 上提供首选项实现的类,该方法用于构建发送到 Windows 注册表的值是一个静态方法 toWindowsName。在 JavaDoc 中...

/**
 * Converts value's or node's name to its Windows representation
 * as a byte-encoded string.
 * Two encodings, simple and altBase64 are used.
 * <p>
 * <i>Simple</i> encoding is used, if java string does not contain
 * any characters less, than 0x0020, or greater, than 0x007f.
 * Simple encoding adds "/" character to capital letters, i.e.
 * "A" is encoded as "/A". Character '\' is encoded as '//',
 * '/' is encoded as '\'.
 * The constructed string is converted to byte array by truncating the
 * highest byte and adding the terminating <tt>null</tt> character.
 * <p>
 * <i>altBase64</i>  encoding is used, if java string does contain at least
 * one character less, than 0x0020, or greater, than 0x007f.
 * This encoding is marked by setting first two bytes of the
 * Windows string to '/!'. The java name is then encoded using
 * byteArrayToAltBase64() method from
 * Base64 class.
 */

因此,简单编码将为大写字母添加正斜杠。

有谁知道为什么需要这样做?我原以为 Registry 可以处理区分大小写的值,但这似乎表明它不能?

我可以解决这个问题,我只是好奇为什么这样做。

最佳答案

我很好奇你和我发现了以下解释:

注册表键保留大小写,但不区分大小写。例如,如果你有一把 key “Rbi”,你就不能制作另一个名为“RBi”的 key 。案例已保存但被忽略。 Sun 的区分大小写的解决方案是在 key 中添加斜杠。

注册表值区分大小写(当然,保留大小写)。我不认为 Sun 有意将斜杠也添加到值中,但它以某种方式溜进了代码中。在我看来,很长一段时间都没有发现这个错误。当发现错误时,许多系统已经依赖于错误的实现,因此他们从未删除它以保持兼容性。

如果您不喜欢 Registry-Values 中的斜杠,您可能会对 this implementation 感兴趣.

关于java - 在 Java 中,为什么 WindowsPreferences 对大写字母使用斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23001152/

相关文章:

c++ - Qt 内部线程

c++ - 这段代码是一个有效的临界区包装类吗

c# - 创建注册表项时出现 UnauthorizedAccessException

java - 从字符串输入构建树

java - 引用 XML 文件中的 Java 属性

c++ - 有没有办法查询消息过滤器是否已经生效?

c++ - RegSetValueEx 静默无法写入 HKLM

powershell - 更改注册表文件夹权限会引发访问被拒绝

Java Web 服务 : sending files using DataHandler class

java - java类崩溃中的startActivity