Java讀取外部資源的方法

Java代碼中經常有讀取外部資源的要求:如配置文件等等,通常會把配置文件放在classpath下或者在web項目中放在web-inf下.

1.從當前的工作目錄中讀取:

[java] view plain copy
  1. try {  
  2.             BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("wkdir.txt")));  
  3.             String str;  
  4.             while ((str = in.readLine()) != null) {  
  5.                 System.out.println(str);  
  6.             }  
  7.             in.close();  
  8.         } catch (IOException e) {  
  9.         }  

2,從classpath中讀取(讀取找到的第一個符合名稱的文件):

[java] view plain copy
  1. try {  
  2.             InputStream stream = ClassLoader.getSystemResourceAsStream("fileinjar.txt");  
  3.             BufferedReader in = new BufferedReader(new InputStreamReader(stream));  
  4.             String str;  
  5.             while ((str = in.readLine()) != null) {  
  6.                 System.out.println(str);  
  7.             }  
  8.             in.close();  
  9.         } catch (IOException e) {  
  10.         }  

3,從classpath中讀取(讀取找到的所有符合名稱的文件,如spring中帶有classpath*:前綴的情況就會從classpath中遍歷):

[java] view plain copy
  1. try {  
  2.   
  3.             Enumeration resourceUrls = Thread.currentThread().getContextClassLoader().getResources("fileinjar.txt");  
  4.   
  5.             while (resourceUrls.hasMoreElements()) {  
  6.                 URL url = (URL) resourceUrls.nextElement();  
  7.                 System.out.println(url);  
  8.   
  9.                 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));  
  10.                 String str;  
  11.                 while ((str = in.readLine()) != null) {  
  12.                     System.out.println(str);  
  13.                 }  
  14.                 in.close();  
  15.   
  16.             }  
  17.   
  18.         } catch (IOException e) {  
  19.         }  

4,從URL中讀取:

[java] view plain copy
  1. try {  
  2.   
  3.             URL url = new URL("http://blog.csdn.net/kkdelta");  
  4.             System.out.println(url);  
  5.   
  6.             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));  
  7.             String str;  
  8.             while ((str = in.readLine()) != null) {  
  9.                 System.out.println(str);  
  10.             }  
  11.             in.close();  
  12.   
  13.         } catch (IOException e) {  
  14.             e.printStackTrace();  
  15.         }  

5,web項目從web-inf文件夾讀取(通過得到ServletContext讀取,可以在servlet或者能夠得到request的類中使用):

[java] view plain copy
  1. try {  
  2.   
  3.             URL url = (URL) getServletContext().getResource("/WEB-INF/webinffile.txt");  
  4.             // URL url = (URL)req.getSession().getServletContext().getResource("/WEB-INF/webinffile.txt");  
  5.             System.out.println(url);  
  6.   
  7.             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));  
  8.             String str;  
  9.             while ((str = in.readLine()) != null) {  
  10.                 System.out.println(str);  
  11.             }  
  12.             in.close();  
  13.   
  14.         } catch (IOException e) {  
  15.             e.printStackTrace();  
  16.         }  

 以上代碼在eclipse環境中運行測試過.不過最近在用JUnit的時候,通過ant運行JUnit時通過ClassLoader.getSystemResourceAsStream("file.txt");的方式去找不到文件.改成 Xclass.class.getClassLoader().getResourceAsStream("file.txt");能從ant指定的classpath中找到文件.原因是ClassLoader和Xclass.class.getClassLoader()是不同的,查找的路徑不一樣.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章