docker容器读取文件问题总结
背景:spring boot项目,打包方式为jar包,以下代码可以正常读取到resources路径下的文件
String filePath = "/rule/cms_rule.json"; // 注意路径前的斜杠,表示类路径的根目录try {InputStream inputStream = new ClassPathResource(filePath).getInputStream();if (inputStream != null) {Scanner scanner = new Scanner(inputStream);while (scanner.hasNextLine()) {System.out.println(scanner.nextLine());}scanner.close();} else {System.out.println("Resource file not found!");}}catch (Exception e){logger.error("读取文件异常",e);}