<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    如風隨影

    統計

    留言簿

    閱讀排行榜

    評論排行榜

    獲取WEB-INF/classes目錄下配置文件的Java類

    通常情況下我們將配置文件放在WEB-INF/classes,如何獲取這個路徑有很多種途徑,下面這個類是我通常使用的類,基本上覆蓋了所有的文件讀取流。
      1import java.io.*;
      2import java.net.*;
      3import java.util.*;
      4
      5/** A class to simplify access to resources through the classloader.
      6@author lavender
      7*/

      8public class Resources extends Object {
      9
     10/** Returns the URL of the resource on the classpath
     11   * @param resource The resource to find
     12   * @throws IOException If the resource cannot be found or read
     13   * @return The resource
     14   */

     15public static URL getResourceURL(String resource) throws IOException {
     16    URL url = null;
     17    ClassLoader loader = Resources.class.getClassLoader();
     18    if (loader != null) url = loader.getResource(resource);
     19    if (url == null) url = ClassLoader.getSystemResource(resource);
     20    if (url == nullthrow new IOException("Could not find resource " + resource);
     21    return url;
     22}

     23
     24/** Returns the URL of the resource on the classpath
     25   * @param loader The classloader used to load the resource
     26   * @param resource The resource to find
     27   * @throws IOException If the resource cannot be found or read
     28   * @return The resource
     29   */

     30public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
     31    URL url = null;
     32    if (loader != null) url = loader.getResource(resource);
     33    if (url == null) url = ClassLoader.getSystemResource(resource);
     34    if (url == nullthrow new IOException("Could not find resource " + resource);
     35    return url;
     36}

     37
     38/** Returns a resource on the classpath as a Stream object
     39   * @param resource The resource to find
     40   * @throws IOException If the resource cannot be found or read
     41   * @return The resource
     42   */

     43public static InputStream getResourceAsStream(String resource) throws IOException {
     44    InputStream in = null;
     45    ClassLoader loader = Resources.class.getClassLoader();
     46    if (loader != null) in = loader.getResourceAsStream(resource);
     47    if (in == null) in = ClassLoader.getSystemResourceAsStream(resource);
     48    if (in == nullthrow new IOException("Could not find resource " + resource);
     49    return in;
     50}

     51
     52/** Returns a resource on the classpath as a Stream object
     53   * @param loader The classloader used to load the resource
     54   * @param resource The resource to find
     55   * @throws IOException If the resource cannot be found or read
     56   * @return The resource
     57   */

     58public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
     59    InputStream in = null;
     60    if (loader != null) in = loader.getResourceAsStream(resource);
     61    if (in == null) in = ClassLoader.getSystemResourceAsStream(resource);
     62    if (in == nullthrow new IOException("Could not find resource " + resource);
     63    return in;
     64}

     65
     66/** Returns a resource on the classpath as a Properties object
     67   * @param resource The resource to find
     68   * @throws IOException If the resource cannot be found or read
     69   * @return The resource
     70   */

     71public static Properties getResourceAsProperties(String resource)
     72      throws IOException {
     73    Properties props = new Properties();
     74    InputStream in = null;
     75    String propfile = resource;
     76    in = getResourceAsStream(propfile);
     77    props.load(in);
     78    in.close();
     79    return props;
     80}

     81
     82/** Returns a resource on the classpath as a Properties object
     83   * @param loader The classloader used to load the resource
     84   * @param resource The resource to find
     85   * @throws IOException If the resource cannot be found or read
     86   * @return The resource
     87   */

     88public static Properties getResourceAsProperties(ClassLoader loader, String resource)
     89      throws IOException {
     90    Properties props = new Properties();
     91    InputStream in = null;
     92    String propfile = resource;
     93    in = getResourceAsStream(loader, propfile);
     94    props.load(in);
     95    in.close();
     96    return props;
     97}

     98
     99/** Returns a resource on the classpath as a Reader object
    100   * @param resource The resource to find
    101   * @throws IOException If the resource cannot be found or read
    102   * @return The resource
    103   */

    104public static InputStreamReader getResourceAsReader(String resource) throws IOException {
    105    return new InputStreamReader(getResourceAsStream(resource));
    106}

    107
    108/** Returns a resource on the classpath as a Reader object
    109   * @param loader The classloader used to load the resource
    110   * @param resource The resource to find
    111   * @throws IOException If the resource cannot be found or read
    112   * @return The resource
    113   */

    114public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {
    115    return new InputStreamReader(getResourceAsStream(loader, resource));
    116}

    117
    118/** Returns a resource on the classpath as a File object
    119   * @param resource The resource to find
    120   * @throws IOException If the resource cannot be found or read
    121   * @return The resource
    122   */

    123public static File getResourceAsFile(String resource) throws IOException {
    124    return new File(getResourceURL(resource).getFile());
    125}

    126
    127/** Returns a resource on the classpath as a File object
    128   * @param loader The classloader used to load the resource
    129   * @param resource The resource to find
    130   * @throws IOException If the resource cannot be found or read
    131   * @return The resource
    132   */

    133public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
    134    return new File(getResourceURL(loader, resource).getFile());
    135}

    136
    137}

    posted on 2008-08-19 10:10 iamlavender 閱讀(2592) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 巨胸喷奶水视频www网免费| 又爽又黄无遮挡高清免费视频 | 亚洲精品视频在线| 久久青青成人亚洲精品| 国产亚洲精品观看91在线| 亚洲黄色免费在线观看| 国产精品亚洲va在线观看| 久久青草免费91线频观看不卡 | 99精品全国免费观看视频| AV在线亚洲男人的天堂| 中文无码亚洲精品字幕| 四虎国产精品永久免费网址| 亚洲精品一级无码鲁丝片 | 一级做a免费视频观看网站| 免费看AV毛片一区二区三区| 亚洲免费黄色网址| 99re免费视频| 亚洲综合精品香蕉久久网97| 美女网站在线观看视频免费的| 免费一级毛片女人图片| 亚洲精品无码久久久久A片苍井空 亚洲精品无码久久久久YW | 亚洲一卡2卡3卡4卡国产网站| 免费人成黄页在线观看日本| 亚洲尤码不卡AV麻豆| 久久久久久久久久久免费精品| 免费一级毛片在级播放| 一级毛片正片免费视频手机看 | 亚洲高清偷拍一区二区三区| 国产精品亚洲а∨无码播放不卡| 四虎永久免费网站免费观看| 国产亚洲美女精品久久久久| 亚洲成A人片在线观看中文| 国产免费区在线观看十分钟| 亚洲精品自产拍在线观看| 暖暖在线视频免费视频| 97久久精品亚洲中文字幕无码| 在线观看永久免费| 国产成人精品日本亚洲网址| 日韩成人免费在线| 成人久久久观看免费毛片| 亚洲热线99精品视频|