如何推荐在Springboot项目中高效读取Resources目录下的文件?

2026-05-16 00:471阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计372个文字,预计阅读时间需要2分钟。

如何推荐在Springboot项目中高效读取Resources目录下的文件?

要求描述:在企业发展过程中,经常需要将一些静态文本数据放置到Resources目录下,项目启动时或程序运行中,需要读取这些文件。

读取Resources目录下文件的实现方法:java/** * 读取Resources目录下文件的内容 * @param fileName 文件名 * @return 文件内容 */public String readFileFromResources(String fileName) { StringBuilder content=new StringBuilder(); try { // 获取Resources目录的路径 URL resource=getClass().getClassLoader().getResource(fileName); if (resource==null) { throw new IllegalArgumentException(文件不存在: + fileName); } // 获取文件路径 File file=new File(resource.getPath()); // 读取文件内容 BufferedReader reader=new BufferedReader(new FileReader(file)); String line; while ((line=reader.readLine()) !=null) { content.append(line).append(\n); } reader.close(); } catch (IOException e) { e.printStackTrace(); } return content.toString();}

需求描述:企业开发过程中,经常需要将一些静态文本数据放到Resources目录下,项目启动时或者程序运行中,需要读取这些文件。

读取Resources目录下文件的方法

/** * @Description: 读取resources 目录下的文件 * @Author: ljj * @CreateDate: 2020/11/3 17:20 * @UpdateUser: * @UpdateDate: * @UpdateReakem * @param filePath * @Return: java.lang.String **/ public static String getContent(String filePath){ String res = ""; if(StringUtils.isEmpty(filePath)){ log.info("文件路径不能为空"); return res; } try { Resource resource = new ClassPathResource(filePath); BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(),"UTF-8")); StringBuffer sb = new StringBuffer(); String str = ""; while((str=br.readLine())!=null) { sb.append(str); } res = sb.toString(); } catch (Exception e) { log.info("读取文件{}时发生异常",filePath); e.printStackTrace(); } return res; }

需要调用时:

String Content = FileUtils.getContent("testData/网元拓扑1.json");

如何推荐在Springboot项目中高效读取Resources目录下的文件?

注意:testData 路径前面没有"/"

到此这篇关于Springboot 项目读取Resources目录下的文件的文章就介绍到这了,更多相关Springboot读取Resources文件内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计372个文字,预计阅读时间需要2分钟。

如何推荐在Springboot项目中高效读取Resources目录下的文件?

要求描述:在企业发展过程中,经常需要将一些静态文本数据放置到Resources目录下,项目启动时或程序运行中,需要读取这些文件。

读取Resources目录下文件的实现方法:java/** * 读取Resources目录下文件的内容 * @param fileName 文件名 * @return 文件内容 */public String readFileFromResources(String fileName) { StringBuilder content=new StringBuilder(); try { // 获取Resources目录的路径 URL resource=getClass().getClassLoader().getResource(fileName); if (resource==null) { throw new IllegalArgumentException(文件不存在: + fileName); } // 获取文件路径 File file=new File(resource.getPath()); // 读取文件内容 BufferedReader reader=new BufferedReader(new FileReader(file)); String line; while ((line=reader.readLine()) !=null) { content.append(line).append(\n); } reader.close(); } catch (IOException e) { e.printStackTrace(); } return content.toString();}

需求描述:企业开发过程中,经常需要将一些静态文本数据放到Resources目录下,项目启动时或者程序运行中,需要读取这些文件。

读取Resources目录下文件的方法

/** * @Description: 读取resources 目录下的文件 * @Author: ljj * @CreateDate: 2020/11/3 17:20 * @UpdateUser: * @UpdateDate: * @UpdateReakem * @param filePath * @Return: java.lang.String **/ public static String getContent(String filePath){ String res = ""; if(StringUtils.isEmpty(filePath)){ log.info("文件路径不能为空"); return res; } try { Resource resource = new ClassPathResource(filePath); BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(),"UTF-8")); StringBuffer sb = new StringBuffer(); String str = ""; while((str=br.readLine())!=null) { sb.append(str); } res = sb.toString(); } catch (Exception e) { log.info("读取文件{}时发生异常",filePath); e.printStackTrace(); } return res; }

需要调用时:

String Content = FileUtils.getContent("testData/网元拓扑1.json");

如何推荐在Springboot项目中高效读取Resources目录下的文件?

注意:testData 路径前面没有"/"

到此这篇关于Springboot 项目读取Resources目录下的文件的文章就介绍到这了,更多相关Springboot读取Resources文件内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!