Java读取Properties文件时碰到两问题
1. 资源文件中的key对应的value过长时,书写不方便,需要换行,若直接回车则回车后的内容被忽略
2.资源文件中的key对应的value需要换行显示时,若直接回车,则同样丢掉回车后的部分
针对上述问题找到如下解决办法:
1. 内容过长需要换行时拼接个/斜杠,这样/后的内容后正常显示
2.若内容本身需要换行时则用/n代替回车
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