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