目录 1.获取方式1 2.示例1 3.获取方式2 4.示例2 1.获取方式1 使用ClassPathResource获取路径下的文件。 一般来说,我们项目的配置文件及静态资源都会放置在resources目录下。有时我们在项目中使用到r
目录
使用ClassPathResource获取路径下的文件。
一般来说,我们项目的配置文件及静态资源都会放置在resources目录下。有时我们在项目中使用到resources目录下的文件,这时我们可以使用spring下的Resouce接口来读取。具体代码如下
Resource resource = new ClassPathResource(“static/Std_Resource_Train_Model.xls”);
// 因为Resouce是一个接口 所以我们可以使用它的实现类ClassPathResource来new一个对象。而构造方法的参数便是resources目录下的文件路径,注意这里是使用的相对路径(相对于resouces目录而言的)。
文件位置
service层
public void export() throws ioException {// String path = "classpath:templates/shelfimport.xlsx";// InputStream inputStream = new ClassPathResource("templates/shelfimport.xlsx").getInputStream();// String path1 = new ClassPathResource("templates/shelfimport.xlsx").getPath(); String filename = new ClassPathResource("templates/shelfimport.xlsx").getFilename(); InputStream inputStream = new ClassPathResource("templates/shelfimport.xlsx").getInputStream(); DynamicHeaderListener listener = new DynamicHeaderListener(); Easyexcel.read(inputStream, listener).sheet().headRowNumber(1).doReadSync(); List
使用当前类的getClass方法获取相应的文件。
String path = "template/shelfimport.xlsx"; InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(path); DanyListener danyListener = new DanyListener(); EasyExcel.read(resourceAsStream, danyListener).sheet(0).headRowNumber(1).doRead(); List<HashMap> dataList = danyListener.getDataList(); dataList.forEach((item) -> { item.forEach((K, V) -> { System.out.println(K); System.out.println(V); }); });
来源地址:https://blog.csdn.net/m0_72167535/article/details/128982729
--结束END--
本文标题: Java中获取某个目录下文件的方式
本文链接: https://www.lsjlt.com/news/400738.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0