处理Properties文件中key包含空格的情况

在这个互联网信息共享的时代,好处是一个问题的很多解决方案都可以从网络上得到,不好的一点就是很多人喜欢复制粘贴也不注明转载出处,不尊重别人的劳动成果,不假思索地把别人的原创复制到自己的博客然后发布,请大家支持原创,原创精神是很重要的,至少证明你动过脑筋去想怎么解决这个问题。

我在百度上用这个关键字搜索“资源文件key空格”,搜出来了好几篇博文,我甚至不知道原创是谁的,真的有必要这样复制粘贴吗?

以上有点扯远了,下面我就来阐述一下这个问题的解决方案。由于某种原因,我的资源文件的key需要是这样的:

res.properties

main form firstName=Stone
main form lastName=Feng

如果你不对资源文件的key里面的空格进行处理而使用java.util.Properties工具进行操作的话,拿出来的value不是你想要的:

测试类

    @Test
public void test() throws IOException {
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("res.properties")); String value = properties.getProperty("main form lastName");
System.out.println("Value is: " + value);
}

结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOsAAAA6CAIAAADeCjkIAAAE90lEQVR4nO2c3YGrIBCFbUsLoh0t4VZBMxST+2BUhvkBNK472XPeVmEYhg8YSLLDC4I8a3jaAQi6JBAM+RYIhnwLBEO+9XsJjmEYxjk903iax2EI8ZnGnejRATr0awlO8+ib4DSPw+NDHMOw6ugL6VoMvJuNfbcGaO17rfu7c9TB7C33Yrc8bA2cIFg2/ft0xc/rBKd5HEIINYSvB9O0ELkDnyK46kaaR7X3K4h5+RjKsuIaT42meRxAsKjLBK+N1838SYIb8w/JQPkstazBaR4334o1vJgM7Onb1bi+HOdERvYonNU+gpB18x1TsRht+6SfW3u7mZmOogCCrW08LRr6nTxcXE2qFvIalwi2Ir8PkOKGRrC1NtPkRCjJHkWLYCWbYb0mU4qkR++QZ6XXJzmZWYEYBhpcWo5Y3YqlOYhNd/lJTQpu7/A0gZwxUEvn250kL+Ksd7Mwf51gJfJ0JW1egytLPB8XUrZ1Dd7GS2yosJot0awRCsb2pFik5XWXEZzVUoNAXGv3k+9qWgtNIJPKFYS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gWDIt0Aw5FsgGPItEAz5FgiGfAsEQ74FgiHfAsGQb4FgyLdAMORbIBjyLRAM+RYIhnwLBEO+BYIh3/oPmtsRDj5oY3sAAAAASUVORK5CYII=" alt="" />

那么解决这个问题呢?有遇到过这种问题的朋友肯定知道,我们需要对资源文件中包含空格的key进行一些改变:

main\ form\ firstName=Stne
main\ form\ lastName=Feng

改变key之后的结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOgAAAA/CAIAAABl8BO4AAAFMUlEQVR4nO2cwZWjMAyGaQsKcjtQwlbhVvbgy7y31z1sB9kDIViWLMsDJKPk/28TjCzJH7Zskhn+/f368/X7BkGuNABcyKMALuRSABdyKYALuRTAhVwK4EIuBXAhlwK4kEsBXMilAC7kUgAXcik34MYwDOOcXtN5msdhCPE1nTvRkwfIC7hpHn2Dm+ZxeGEEq2IYVu2xkNBi4GEaY9cGaI29Ff7DOepgdjX/8Di4UrA/UUf8PA5umschhNAi93gyVQuRO3AWuE030jxWo1/JztvHULYtZnSAa9FhcNfO22Y+ElxjkUEN9IOb5nFz6bEElAtBfiFzafUwrhfHOZEB3Rtnd++xZ9HdUyk2o31/08+tv4eZmQ6eMP66tmHUIOh3cndxNVm1kN9xCFwt848BqrhRA1ebiWkFQlp2gFupVFiw5AEipc8901nr9ZMcyKxBDAPNKW1HrG7N0hzErrv8pCYFtx/MmPjNhr5VqtudJBfiXA+zMH8c3Erm6bxpnnEbEzofl3tbE7jbMIn2CxezCZk5THnYPimmZHmWZeBmd1VjJ67Z/eRLV60HE7/k5ga5ZidtMQsXj4NbyfwBcO3L1262CS6vm6u29uZMawNORBFf0SAPipcK1EWKe9lzj58CE/q0oMNYPIx6Pu3J3K5Je5hrwa1k/ing7na7ZlyxAyHX2rBcC+7qqTgOZj/t4FqOeehjxJ8n3tqYzIoD3wGXLMUa2ReA23v+u5k5vcbV/LgcXHo/bWf3U3aT9WipccXJWJmhO5JZ8a4XXNJeelLyBleAKxSQqu52Dp0qCA7ztTDNo1y/b5+cDG6xkSDgdvhZDEJZe9vmCK023uvpSlWvOpltQot9msY6c5w5WIRePGN94PKu1BcURdzrn8JDvho6480ZqyRIbcaOwy6tccnJmmDd5GfWdiW2uWYrianuaOWCuiuZquvqcZiyZtDKmu0NbeByNxrFLO21ctqTm3byyhc6Q+al4gJ17sJUxQBwP0rvAu4tnVIqQF70KC/6y54jMu9lOwRwIZfSwP0FQT9VmHEhlwK4kEsBXMilAC7kUu8P7jIN0/Kis0voMj0B3P3d5EsAArhvqTa4y7Rzd7vdytf45p7SMn0CQGnPF9Vzz/zfXtbv4xZpT8u0cWzVh4B7W5PDvxsIcE+VrVRgeSfrL51kanRycO/3bQ/ANpOTVuR72H2PSqtEyd0OUUDrbqD7cdPBrUW0Z2N3jHVNY9ru+IjpoJCxxo0hz0/S6sZlkpdFecZNy0RxzFsVBKSaaVWVmZ79OoKbPg4uexoaEaVlmqZHqour5M+0TMM0fcoaJsi6OcuHv1EnMBa5BaVx1kpaX2PoHas6uFeNOlmByt+GqhGxR1PLhjp9vL3MpwoZYTxh5YbkFHBr25zOObdaWxP7ZxJQnXGbEenZYFn9mF2DoI7jsHuaWAVXzhJnzbgVO70ybQpPnb2EGjdzpXEnwLWp5xw3hiHEclR4Qg+BS1bwUwbGeJpx4qFHFdxmRF2FE0oF8wuIGIYwlYNCUCsOCnK1a83EdskxFIu4wkRNYr/MjlTynniqQGzWI2puVferMWBzZn9zJv/6Lz/g2U9zst/QqXVqVvjtRzzFXlqvnuuuVvvl1Wblt7jd4DJ/pUVGiij7eP2PH8Xf5a0holR46+8qvLEALsB1qBh6X16+kwCuK5EC6KNfIgNcyKUALuRSABdyKYALuRTAhVwK4EIuBXAhlwK4kEsBXMilAC7kUgAXcimAC7kUwIVcCuBCLgVwIZf6D/HqeRMl2Q+CAAAAAElFTkSuQmCC" alt="" />

希望这篇拙文能够帮到有需要的同行。

上一篇:tomcat+mysql数据库连接池的操作


下一篇:Delphi 数据导出到Excel