以读取excel文件为例
项目结构如下
相对路径
//FileInputStream相对路径写法,从src开始到目标文件,下面两种写法二选一
//FileInputStream fis = new FileInputStream("src/main/resources/templates/test.xlsx");
FileInputStream fis = new FileInputStream("./src/main/resources/templates/test.xlsx");
Workbook workbook = WorkbookFactory.create(fis);
绝对路径
//FileInputStream绝对路径写法
FileInputStream fis = new FileInputStream("D:/test.xlsx");
Workbook workbook = WorkbookFactory.create(fis);
也可以使用InputStream相对路径读取
//使用InputStream相对路径
InputStream is = ExcelToPdfZip.class.getClassLoader().getResourceAsStream("templates/test.xlsx");
Workbook workbook = WorkbookFactory.create(is);