温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

类路径下的文件怎么利用Java进行读取

发布时间:2020-11-27 16:44:27 来源:亿速云 阅读:422 作者:Leah 栏目:编程语言

这篇文章将为大家详细讲解有关类路径下的文件怎么利用Java进行读取,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

Java 读取类路径下的文件

一、工具类代码ResourceLoadUtil.java

import java.io.IOException; 
import java.io.InputStream; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.Enumeration; 
import java.util.List; 
 
public class ResourceLoadUtil { 
 
  /** 
   * @param args 
   */ 
  public static void main(String[] args) { 
    System.out.println("ByClazz...\n--------------------------"); 
    System.out.println(loadResourceByClazz(String.class, "/java/lang/String.class")); 
    System.out.println(loadResourceByClazz(String.class, "/ResourceLoadUtil.class")); 
    System.out.println(); 
 
    System.out.println("ByClasLoader...\n--------------------------"); 
    System.out.println(loadResourceByClassLoader("java/lang/String.class")); 
    System.out.println(loadResourceByClassLoader("ResourceLoadUtil.class")); 
    System.out.println(); 
 
    System.out.println("sByClasLoader...\n--------------------------"); 
    System.out.println(loadResourcesByClassLoader("java/lang/String.class")); 
    System.out.println(loadResourcesByClassLoader("ResourceLoadUtil.class")); 
    System.out.println(loadResourcesByClassLoader("struts-plugin.xml")); 
 
  } 
 
  /** 
   * 根据Class加载资源文件 
   * 
   * @param clazz 
   * @param path 
   *      以'/'开头的路径: 使用绝对路径寻找文件(以ClassPath为根路径开始)<br> 
   *      否则: 使用相对路径寻找文件(相对于clazz位置路径开始) 
   * @return 
   */ 
  public static InputStream loadResourceByClazz(Class<?> clazz, String path) { 
    return clazz.getResourceAsStream(path); 
  } 
 
  /** 
   * 根据ClassLoader加载资源文件(单个), 使用默认的ClassLoader! 
   * 
   * @param path 
   *      不能以'/'开头的路径 
   * @return 
   */ 
  public static InputStream loadResourceByClassLoader(String path) { 
    return loadResourceByClassLoader(null, path); 
  } 
 
  /** 
   * 根据ClassLoader加载资源文件(单个), 使用默认的ClassLoader! 
   * 
   * @param path 
   *      不能以'/'开头的路径 
   * @return 
   */ 
  public static InputStream loadResourceByClassLoader(ClassLoader cl, String path) { 
    if (cl == null) { 
      cl = Thread.currentThread().getContextClassLoader(); 
    } 
    return cl.getResourceAsStream(path); 
  } 
 
  /** 
   * 根据ClassLoader加载资源文件(多个),使用默认的ClassLoader! 
   * 
   * @param path 
   *      不能以'/'开头的路径 
   * @return 
   */ 
  public static List<URL> loadResourcesByClassLoader(String path) { 
    return loadResourcesByClassLoader(null, path); 
  } 
 
  /** 
   * 根据ClassLoader加载资源文件(多个),使用指定的ClassLoader! 
   * 
   * @param path 
   *      不能以'/'开头的路径 
   * @return 
   */ 
  public static List<URL> loadResourcesByClassLoader(ClassLoader cl, String path) { 
    if (cl == null) { 
      cl = Thread.currentThread().getContextClassLoader(); 
    } 
    List<URL> urlLst = new ArrayList<URL>(); 
    try { 
      Enumeration<URL> urlsEnum = cl.getResources(path); 
      while (urlsEnum.hasMoreElements()) { 
        URL url = (URL) urlsEnum.nextElement(); 
        urlLst.add(url); 
      } 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
    return urlLst; 
  } 
}

二、执行结果

ByClazz... 
-------------------------- 
sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@11cfb549 
java.io.BufferedInputStream@a422ede 
 
ByClasLoader... 
-------------------------- 
sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@7f12f614 
java.io.BufferedInputStream@35d9dc39 
 
sByClasLoader... 
-------------------------- 
[jar:file:/D:/Java/jdk1.6.0_25/jre/lib/rt.jar!/java/lang/String.class] 
[file:/E:/workspace/JBDS/Spring+Struts2-Integration/target/classes/ResourceLoadUtil.class] 
[jar:file:/E:/360data/%e9%87%8d%e8%a6%81%e6%95%b0%e6%8d%ae/.m2/repository/org/apache/struts/struts2-spring-plugin/2.3.4.1/struts2-spring-plugin-2.3.4.1.jar!/struts-plugin.xml, jar:file:/E:/360data/%e9%87%8d%e8%a6%81%e6%95%b0%e6%8d%ae/.m2/repository/org/apache/struts/struts2-config-browser-plugin/2.3.4.1/struts2-config-browser-plugin-2.3.4.1.jar!/struts-plugin.xml]

关于类路径下的文件怎么利用Java进行读取就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI