赞
踩
ClassPathResource classPathResource = new ClassPathResource("text/test.txt");
InputStream inputStream =classPathResource.getInputStream();
InputStream inputStream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("text/test.txt");
InputStream inputStream =
ClassLoader.getSystemClassLoader().getResourceAsStream("xls010.xls")
InputStream inputStream = this.getClass().getResourceAsStream("/text/test.txt");
File file = ResourceUtils.getFile("classpath:text/test.txt");
InputStream inputStream = new FileInputStream(file);
前4种方法在项目文件或jar包时都能读取到, 原理是通过类加载器读取
第5种只能在项目文件中读取
new ApplicationHome().getDir().getPath();
方法1就是调用了方法2 System.getProperty("user.dir")
System.getProperty("user.dir")
String源码
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。