Java讀取外部資源的方法詳解及實(shí)例代碼

Java 讀取外部資源的方法詳解

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),蜀山企業(yè)網(wǎng)站建設(shè),蜀山品牌網(wǎng)站建設(shè),網(wǎng)站定制,蜀山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,蜀山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

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

1.從當(dāng)前的工作目錄中讀取:

try { 
      BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("wkdir.txt"))); 
      String str; 
      while ((str = in.readLine()) != null) { 
        System.out.println(str); 
      } 
      in.close(); 
    } catch (IOException e) { 
    } 

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

try { 
      InputStream stream = ClassLoader.getSystemResourceAsStream("fileinjar.txt"); 
      BufferedReader in = new BufferedReader(new InputStreamReader(stream)); 
      String str; 
      while ((str = in.readLine()) != null) { 
        System.out.println(str); 
      } 
      in.close(); 
    } catch (IOException e) { 
    } 

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

try { 
 
      Enumeration resourceUrls = Thread.currentThread().getContextClassLoader().getResources("fileinjar.txt"); 
 
      while (resourceUrls.hasMoreElements()) { 
        URL url = (URL) resourceUrls.nextElement(); 
        System.out.println(url); 
 
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
        String str; 
        while ((str = in.readLine()) != null) { 
          System.out.println(str); 
        } 
        in.close(); 
 
      } 
 
    } catch (IOException e) { 
    } 

4,從URL中讀取:

try { 
 
      URL url = new URL("http://blog.csdn.net/kkdelta"); 
      System.out.println(url); 
 
      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
      String str; 
      while ((str = in.readLine()) != null) { 
        System.out.println(str); 
      } 
      in.close(); 
 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 

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

try { 
 
      URL url = (URL) getServletContext().getResource("/WEB-INF/webinffile.txt"); 
      // URL url = (URL)req.getSession().getServletContext().getResource("/WEB-INF/webinffile.txt"); 
      System.out.println(url); 
 
      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
      String str; 
      while ((str = in.readLine()) != null) { 
        System.out.println(str); 
      } 
      in.close(); 
 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 

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

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

當(dāng)前標(biāo)題:Java讀取外部資源的方法詳解及實(shí)例代碼
當(dāng)前鏈接:http://muchs.cn/article36/gpghpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、商城網(wǎng)站、網(wǎng)站維護(hù)、做網(wǎng)站、網(wǎng)站建設(shè)定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)