歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java讀取資源文件時內容過長與換行的處理

Java讀取資源文件時內容過長與換行的處理

日期:2017/3/1 11:08:30   编辑:Linux編程

Java讀取Properties文件時碰到兩問題

1. 資源文件中的key對應的value過長時,書寫不方便,需要換行,若直接回車則回車後的內容被忽略

2.資源文件中的key對應的value需要換行顯示時,若直接回車,則同樣丟掉回車後的部分

針對上述問題找到如下解決辦法:

1. 內容過長需要換行時拼接個/斜槓,這樣/後的內容後正常顯示

2.若內容本身需要換行時則用/n代替回車

  1. package apistudy;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.Properties;
  5. public class PropertiesTest2
  6. {
  7. public static void main(String[] args)
  8. {
  9. Properties properties = new Properties();
  10. try
  11. {
  12. InputStream inputStream = PropertiesTest2.class.getClassLoader().getResourceAsStream("test.properties");
  13. properties.load(inputStream);
  14. inputStream.close(); //關閉流
  15. }
  16. catch (IOException e)
  17. {
  18. e.printStackTrace();
  19. }
  20. String key1 = properties.getProperty("key1");
  21. String key2 = properties.getProperty("key2");
  22. System.out.println(key1);
  23. System.out.println(key2);
  24. }
  25. }

輸出結果:

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

Copyright © Linux教程網 All Rights Reserved