根据资源的位置访问资源
1.类路径下:
Class.getResourceAsStream(String name) :name以 "/"开头表示从类路径下获取,否则表示从指定类Class同路径下获取,比如Main.class.getResourceAsStream("a.xml")Main全类名为com.Main,则表示获取类路径下com包下的a.xml文件
ClassLoader.getResourceAsStream(String name):Main.class.getClassLoader().getResourceAsStream("a.xml") 表示从类路径下获取a.xml, Main.class.getClassLoader().getResourceAsStream("com/a.xml") 表示获取类路径下com包下的a.xml
ClassPathResource(String path):表示从类路径下获取资源。new ClassPathResource("/a.xml") 和new ClassPathResource("a.xml")是一样的,源码里面会把"/"去掉,都表示获取类路径下的a.xml,new ClassPathResource("com/a.xml")表示获取com包下 a.xml
2.web环境下
根据ServletContext.getRealPath("/")获取项目根目录,然后即可获取到任何子目录下的路径,根路径+“WEB-INF/classes”表示的是项目类路径。
ServletContextResourcePatternResolver(servletContext):spring方式获取
import java.io.IOException;
import java.io.InputStream;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
public class Main {
public static void main(String[] args) throws IOException {
// 1.a.xml在类路径下
InputStream inputStream = Main.class.getResourceAsStream("/a.xml");
InputStream inputStream2 = Main.class.getClassLoader().getResourceAsStream("a.xml");
// spring的Resource获取方式(需要spring的core包)
InputStream inputStream3 = new ClassPathResource("/a.xml").getInputStream();
InputStream inputStream4 = new ClassPathResource("a.xml").getInputStream();
// 2.a.xml在类路径的某个包下,比如com包下
// a.和当前类Main不在同一包
InputStream inputStream5 = Main.class.getResourceAsStream("/com/a.xml");
// b.和当前类Main在同一包
InputStream inputStream6 = Main.class.getResourceAsStream("a.xml");
InputStream inputStream7 = Main.class.getClassLoader().getResourceAsStream("com/a.xml");
// spring的Resource获取方式
InputStream inputStream8 = new ClassPathResource("/com/a.xml").getInputStream();
InputStream inputStream9 = new ClassPathResource("com/a.xml").getInputStream();
byte[] b = new byte[1024];
byte[] b2 = new byte[1024];
byte[] b3 = new byte[1024];
byte[] b4 = new byte[1024];
byte[] b5 = new byte[1024];
byte[] b6 = new byte[1024];
byte[] b7 = new byte[1024];
byte[] b8 = new byte[1024];
byte[] b9 = new byte[1024];
inputStream.read(b);
inputStream2.read(b2);
inputStream3.read(b3);
inputStream4.read(b4);
inputStream5.read(b5);
inputStream6.read(b6);
inputStream7.read(b7);
inputStream8.read(b8);
inputStream9.read(b9);
System.out.println(new String(b));
System.out.println(new String(b2));
System.out.println(new String(b3));
System.out.println(new String(b4));
System.out.println(new String(b5));
System.out.println(new String(b6));
System.out.println(new String(b7));
System.out.println(new String(b8));
System.out.println(new String(b9));
// 3.web环境获取(先获取ServletContext对象)
// 获取当前项目的根目录(其中path + "WEB-INF/classes"得到的就是类路径)
// String path = request.getServletContext().getRealPath("/");
// 如果要读取项目根目录下 doc目录下的文件a.xml
// FileInputStream inputStream10 = new FileInputStream(new File(path + "doc/a.xml"));
// spring的Resource获取方式
// InputStream inputStream11 = new ServletContextResource(servletContext, "doc/a.xml").getInputStream();
// 4.ant风格通配符获取方式(spring)
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// 获取类路径下a.xml文件
InputStream inputStream12 = resolver.getResource("classpath:a.xml").getInputStream();
// 获取类路径下 mapper下的所有xml文件
Resource[] resources = resolver.getResources("classpath:mapper/*.xml");
// 遍历获取所有的xml文件
for(Resource re : resources){
InputStream stream = re.getInputStream();
}
// web环境方式
// ServletContextResourcePatternResolver resolver2= new ServletContextResourcePatternResolver(servletContext);
// 获取项目根路径下doc目录下的所有xml文件
// Resource[] resources2 = resolver2.getResources("doc/*.xml");
}
}