Java读取资源文件时内容过长与换行的解决
发布时间:2021-12-18 17:04:10 所属栏目:教程 来源:互联网
导读:Java读取Properties文件时碰到两问题 1. 资源文件中的key对应的value过长时,书写不方便,需要换行,若直接回车则回车后的内容被忽略 2.资源文件中的key对应的value需要换行显示时,若直接回车,则同样丢掉回车后的部分 针对上述问题找到如下解决办法: 1. 内
Java读取Properties文件时碰到两问题 1. 资源文件中的key对应的value过长时,书写不方便,需要换行,若直接回车则回车后的内容被忽略 2.资源文件中的key对应的value需要换行显示时,若直接回车,则同样丢掉回车后的部分 针对上述问题找到如下解决办法: 1. 内容过长需要换行时拼接个/斜杠,这样/后的内容后正常显示 2.若内容本身需要换行时则用/n代替回车 package apistudy; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesTest2 { public static void main(String[] args) { Properties properties = new Properties(); try { InputStream inputStream = PropertiesTest2.class.getClassLoader().getResourceAsStream("test.properties"); properties.load(inputStream); inputStream.close(); //关闭流 } catch (IOException e) { e.printStackTrace(); } String key1 = properties.getProperty("key1"); String key2 = properties.getProperty("key2"); System.out.println(key1); System.out.println(key2); } } 输出结果: Where did you take the picture? It's so beautiful! Spring Hibernate Ibatis Velocity Java Struts 附:test.properties中的内容 key1=Where did you take the picture? / It's so beautiful! key2=Spring/nHibernate/nIbatis/nVelocity/nJava/nStruts ![]() (编辑:开发网_开封站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |