欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年03月02日 11:34:49   投稿:lqh  
這篇文章主要介紹了Java 讀取外部資源的方法詳解及實(shí)例代碼的相關(guān)資料,經(jīng)常有讀取外部資源的要求,如配置文件等等需要讀取,需要的朋友可以參考下

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

在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()是不同的,查找的路徑不一樣.

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

相關(guān)文章

  • Socket+JDBC+IO實(shí)現(xiàn)Java文件上傳下載器DEMO詳解

    Socket+JDBC+IO實(shí)現(xiàn)Java文件上傳下載器DEMO詳解

    這篇文章主要介紹了Socket+JDBC+IO實(shí)現(xiàn)Java文件上傳下載器DEMO詳解,需要的朋友可以參考下
    2017-05-05
  • 詳解Mybatis是如何把數(shù)據(jù)庫數(shù)據(jù)封裝到對象中的

    詳解Mybatis是如何把數(shù)據(jù)庫數(shù)據(jù)封裝到對象中的

    這篇文章主要介紹了Mybatis是如何把數(shù)據(jù)庫數(shù)據(jù)封裝到對象中的,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • SpringBoot集成Redisson實(shí)現(xiàn)延遲隊(duì)列的場景分析

    SpringBoot集成Redisson實(shí)現(xiàn)延遲隊(duì)列的場景分析

    這篇文章主要介紹了SpringBoot集成Redisson實(shí)現(xiàn)延遲隊(duì)列,本文通過場景分析實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • 最新評論