如風(fēng)隨影

          統(tǒng)計

          留言簿

          閱讀排行榜

          評論排行榜

          獲取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 閱讀(2601) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 宁国市| 阜阳市| 东乌珠穆沁旗| 旌德县| 鄢陵县| 临漳县| 工布江达县| 文昌市| 怀宁县| 贡嘎县| 苏尼特右旗| 浏阳市| 轮台县| 霸州市| 娱乐| 双辽市| 同江市| 汾阳市| 舒兰市| 铜山县| 沿河| 和田县| 蒙自县| 霞浦县| 海盐县| 石狮市| 扎兰屯市| 那曲县| 台州市| 阿图什市| 湘阴县| 玛沁县| 九江市| 同德县| 富锦市| 惠州市| 永仁县| 吴忠市| 龙胜| 衢州市| 灌云县|