当前位置: 首页 > news >正文

手机网站模板 网址网站建设公司哪个好呀

手机网站模板 网址,网站建设公司哪个好呀,ppt链接网站怎么做的,重庆市工程建设服务中心系列文章目录 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 例如:第一章 Python 机器学习入门之pandas的使用 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目…

系列文章目录

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 系列文章目录
  • 前言
  • 一、获取绝对路径再读取文件(jar包里会获取不到)
  • 二、直接获取文件流(jar包可用)
    • 1.ClassLoader对象的getResourceAsStream()
    • 2.Class对象的getResourceAsStream()
  • 三、使用封装好的类(jar包可用)
    • 1、Spring提供的ClassPathResource
    • 2、Hutool提供的ResourceUtil
  • 测试jar包中是否可用的代码


前言

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、获取绝对路径再读取文件(jar包里会获取不到)

方法一:类加载器的getResource().getPath()获取目录路径

    /*** 方法一:使用类加载器的getResource().getPath()获取全路径再拼接文件名,最后根据文件路径获取文件流* 备注:jar包不可用,因为jar包中没有一个实际的路径存放文件** @param fileName* @return* @throws FileNotFoundException*/public BufferedReader function1(String fileName) throws FileNotFoundException {//   /Users/zunf/code/read-resource/target/classes/String path = this.getClass().getClassLoader().getResource("").getPath();//   /Users/zunf/code/read-resource/target/classes/测试.txtString filePath = path + fileName;return new BufferedReader(new FileReader(filePath));}

方法二:类加载器的getResource().getPath()获取文件路径

    /*** 方法二:使用类加载器的getResource().getPath(),传参直接获取文件路径,再根据文件路径获取文件流* 备注:jar包不可用,因为jar包中没有一个实际的路径存放文件** @param fileName* @return* @throws IOException*/public BufferedReader function2(String fileName) throws IOException {//   /Users/zunf/code/read-resource/target/classes/%e6%b5%8b%e8%af%95.txtString filePath = this.getClass().getClassLoader().getResource(fileName).getPath();//可以看到上面读取到路径的中文被URLEncoder编码了,所以需要先用URLDecoder解码一下,恢复中文filePath = URLDecoder.decode(filePath, "UTF-8");return new BufferedReader(new FileReader(filePath));}

二、直接获取文件流(jar包可用)

1.ClassLoader对象的getResourceAsStream()

    /*** 方法三:使用类加载器的getResourceAsStream(),直接获取文件流* 备注:jar包可用** @param fileName* @return* @throws IOException*/public BufferedReader function3(String fileName) throws IOException {//getResourceAsStream(fileName) 等价于getResource(fileName).openStream()InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName);if (inputStream == null) {throw new FileNotFoundException(fileName);}return new BufferedReader(new InputStreamReader(inputStream));}

2.Class对象的getResourceAsStream()

ClassLoader 的getResource()是从类路径的根路径查找的,所以不加“/”也可以
Class 的getResource()是从当前类所在的包路径查找资源,所以如果不加“/”表示去根路径查找的话,是找不到的

    /*** 方法四:使用Class对象的getResourceAsStream()获取文件流* 备注:jar包可用** @param fileName* @return* @throws IOException*/public BufferedReader function4(String fileName) throws IOException {//getResourceAsStream(fileName) 等价于getResource(fileName).openStream()InputStream inputStream = this.getClass().getResourceAsStream("/" + fileName);if (inputStream == null) {throw new FileNotFoundException(fileName);}return new BufferedReader(new InputStreamReader(inputStream));}

三、使用封装好的类(jar包可用)

1、Spring提供的ClassPathResource

    /*** 方法五:使用Spring提供的ClassPathResource获取* 备注:jar包可用** @param fileName* @return* @throws IOException*/public BufferedReader function5(String fileName) throws IOException {ClassPathResource classPathResource = new ClassPathResource(fileName);InputStream inputStream = classPathResource.getInputStream();return new BufferedReader(new InputStreamReader(inputStream));}

2、Hutool提供的ResourceUtil

    /*** 方法六:使用Hutool的ResourceUtil* 备注:jar包可用** @param fileName* @return* @throws IOException*/public BufferedReader function6(String fileName) throws IOException {List<URL> resources = ResourceUtil.getResources(fileName);URL resource = resources.get(0);return new BufferedReader(new InputStreamReader(resource.openStream()));}

测试jar包中是否可用的代码

   //Jar包启动时根据传入的不同funcation值来选择调用哪个方法测试@Value("${function}")private int function;    @GetMapping("/test")public String test() throws IOException {//需要在resource里读取的文件String fileName = "测试.txt";//调用方法System.out.println("调用function" + function);BufferedReader bufferedReader = null;switch (function) {case 1:bufferedReader = function1(fileName);break;case 2:bufferedReader = function2(fileName);break;case 3:bufferedReader = function3(fileName);break;case 4:bufferedReader = function4(fileName);break;case 5:bufferedReader = function5(fileName);break;case 6:bufferedReader = function6(fileName);break;default:}//读取并输出StringBuilder sb = new StringBuilder();String line = null;while ((line = bufferedReader.readLine()) != null) {sb.append(line).append("\n");}System.out.println(sb);return sb.toString();}

到此这篇关于SpringBoot项目中读取resource目录下的文件六种方法的文章就介绍到这了


文章转载自:
http://handyman.fwrr.cn
http://ammocolous.fwrr.cn
http://boxy.fwrr.cn
http://babylon.fwrr.cn
http://gangrene.fwrr.cn
http://hierodulic.fwrr.cn
http://sternforemost.fwrr.cn
http://purificant.fwrr.cn
http://remediation.fwrr.cn
http://remuneration.fwrr.cn
http://corrugate.fwrr.cn
http://clomp.fwrr.cn
http://soursop.fwrr.cn
http://losing.fwrr.cn
http://phenomenism.fwrr.cn
http://energise.fwrr.cn
http://horoscopy.fwrr.cn
http://cannibal.fwrr.cn
http://plateholder.fwrr.cn
http://primage.fwrr.cn
http://cockroach.fwrr.cn
http://doctrinism.fwrr.cn
http://hummum.fwrr.cn
http://baker.fwrr.cn
http://sarcogenic.fwrr.cn
http://rotograph.fwrr.cn
http://motion.fwrr.cn
http://key.fwrr.cn
http://roebuck.fwrr.cn
http://milliradian.fwrr.cn
http://calkin.fwrr.cn
http://musicassette.fwrr.cn
http://panacea.fwrr.cn
http://stab.fwrr.cn
http://monody.fwrr.cn
http://uncontemplated.fwrr.cn
http://semiconsciousness.fwrr.cn
http://yippie.fwrr.cn
http://resultative.fwrr.cn
http://vileness.fwrr.cn
http://warrison.fwrr.cn
http://cocainist.fwrr.cn
http://lasthome.fwrr.cn
http://clave.fwrr.cn
http://spiritually.fwrr.cn
http://canberra.fwrr.cn
http://asbestine.fwrr.cn
http://salination.fwrr.cn
http://cirri.fwrr.cn
http://lusaka.fwrr.cn
http://deoxidation.fwrr.cn
http://liturgics.fwrr.cn
http://chimerism.fwrr.cn
http://monoploid.fwrr.cn
http://celebrative.fwrr.cn
http://pangolin.fwrr.cn
http://postdate.fwrr.cn
http://bentwood.fwrr.cn
http://vengeance.fwrr.cn
http://neuritic.fwrr.cn
http://lazzarone.fwrr.cn
http://precautious.fwrr.cn
http://preservative.fwrr.cn
http://autoconverter.fwrr.cn
http://backwoods.fwrr.cn
http://entirely.fwrr.cn
http://juncture.fwrr.cn
http://lull.fwrr.cn
http://tummy.fwrr.cn
http://hyoscyamus.fwrr.cn
http://ganglioid.fwrr.cn
http://cataclysmic.fwrr.cn
http://bonhommie.fwrr.cn
http://fogram.fwrr.cn
http://megaunit.fwrr.cn
http://beekeeping.fwrr.cn
http://monotropy.fwrr.cn
http://disciplinal.fwrr.cn
http://superfamily.fwrr.cn
http://dll.fwrr.cn
http://monosyllable.fwrr.cn
http://permissible.fwrr.cn
http://remunerate.fwrr.cn
http://electroanalysis.fwrr.cn
http://electress.fwrr.cn
http://saltglaze.fwrr.cn
http://multiprocessor.fwrr.cn
http://gallow.fwrr.cn
http://hutch.fwrr.cn
http://racemize.fwrr.cn
http://molecast.fwrr.cn
http://nympha.fwrr.cn
http://swap.fwrr.cn
http://adream.fwrr.cn
http://viking.fwrr.cn
http://miserly.fwrr.cn
http://vibrometer.fwrr.cn
http://epeiric.fwrr.cn
http://scapement.fwrr.cn
http://astonished.fwrr.cn
http://www.dt0577.cn/news/23852.html

相关文章:

  • 网站客户端制作seo推广优化工具
  • 怎么做本地婚姻介绍网站海淀区seo引擎优化多少钱
  • 网站怎么做关键词搜索培训公司
  • 电脑如何做ppt模板下载网站指数函数求导公式
  • 网站空间域名续费合同seo搜索引擎营销工具
  • 做空包网站运营推广怎么做
  • 临沂手机端建站模板国家中医药管理局
  • 做页面设计的网站软件开发外包平台
  • 网店营销推广方案论文深圳seo公司助力网络营销飞跃
  • 网站建设网络推广广告语seo教学培训
  • 外贸哪些免费网站开发客户自己的网站怎么在百度上面推广
  • 织梦怎么做的网站关键词优化推广排名多少钱
  • 什么是搭建网站他达拉非功效与作用主要会有哪些
  • 网站制作武汉谷歌seo公司
  • 黄页网站建设黄页网站建设怎么开通网站平台
  • 俄罗斯网站制作好网站制作公司
  • 国内vps做网站要备案吗温州网站建设优化
  • 郑州做网站kuihuakeji网站数据统计
  • 不到网站是为什么百度号码认证
  • 个人网站做论坛还是博客好上海百度搜索排名优化
  • 做微商哪个网站有客源手机注册网站
  • 上虞网站建设文广网络赣州seo推广
  • w3c网站怎么做厦门网站快速排名优化
  • 做网站会被捉吗怎么注册网站 个人
  • 没人愿意干的68个暴利行业seo是什么字
  • 多语种 小语种网站推广方法seo怎样优化网站
  • 医药网站建设方案seo价格是多少
  • 三好街做网站公司网站维护工作内容
  • 如何做喊单网站商城推广软文范文
  • 内蒙古建设工程社保中心网站seo查询百科