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

专业的企业智能建站比较好武汉网站运营专业乐云seo

专业的企业智能建站比较好,武汉网站运营专业乐云seo,怎么做婚庆网站平台,科技感十足的网站前言 本地的可修改配置文件的编写理应是一个很常用的功能,但由于数据库的存在,它鲜少被提及,大多数我们直接存储到数据库中了。 以至于现今,除了没接触数据库的新手时常使用它以外,它没有太多的出场机会。 也因此&am…

前言

本地的可修改配置文件的编写理应是一个很常用的功能,但由于数据库的存在,它鲜少被提及,大多数我们直接存储到数据库中了。

以至于现今,除了没接触数据库的新手时常使用它以外,它没有太多的出场机会。
也因此,其实很多人不太了解其中的存储机制以及可能存在的一些坑。

不过也有适合用本地配置的时候,例如我遇到了一个场景,需要保存一个邮箱格式,这个格式当然不会经常变化,因而存储到数据库不太方便,尤其我用的是关系型数据库MYSQL,存起来感觉很别扭。如果存到Redis中,一旦服务宕机,数据就会消失,虽然并不是很重要的数据,但也不是很好的解决方案。

分析

要了解如何生成一个本地的可编辑配置文件,首先就得想清楚文件应该放在哪?

新手可能会直接回答,这还不简单,直接跟代码一样放classes目录下不就得了。

答案显然是不行的,因为jar包本质上是一个压缩包,如果你放在IDE中直接运行,那么不会有任何问题,但是一旦你需要打jar包或者war包。你就无法动态的去修改Jar包中的内容,这点很容易理解。

所以,比较科学的方法,就是在Jar/War包的同级目录下,新建一个配置文件。

1.获取Jar包同级目录

如何获取Jar包的同级目录,可以使用jdk提供的Protection Domain
以下代码可以得到当前代码源的位置,简单来说,就是编译生成的classes目录。

File file=new File file=new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile());

但是要得到想要的Jar包路径还没有那么简单,因为这里有一个坑,在IDE中直接运行和打成Jar包后运行的效果是不同的。

例如以下的这段演示代码:

String name = new String("history.json");
File file=new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile());
System.out.println(file.toString());
String jarPath = file.getParentFile().getParent();//jar所在文件夹路径
System.out.println("jarPath: " + jarPath);
String resource = jarPath + File.separator +name;
System.out.println(resource);

(1)IDE中

在这里插入图片描述

(2)windows下运行Jar

在这里插入图片描述

(3)Linux下运行Jar

在这里插入图片描述

路径问题总结

由此,我们得出一个结论,上述获取jar包路径的代码,在IDE中运行的效果和打包后运行效果不同,且打包后Windows和Linux运行效果基本一致。

我们想要的Jar文件夹路径,需要向上获取三次父目录,正确的在打包后获取Jar包路径的代码应该如下:
resource即我们需要的配置文件路径,使用File.seperator是为了不同平台的适配性。

String name = new String("history.json");
File file=new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile());
File jarPath = file.getParentFile().getParentFile().getParentFile().getParentFile();//jar所在文件夹路径
String resource = jarPath.getPath().replace("file:","") + File.separator +name;

2.读取与修改配置文件

有了对应的配置文件路径,那么就很轻松了。

考虑到配置文件一般是Properties或Yaml格式,这里以Java本身支持的Properties为例。

使用字符流读取较为方便,因其可以自动处理编码问题。

例如:

 //使用propertiesReader reader =new FileReader(config);Properties properties = new Properties();properties.load(reader);System.out.println(properties.getProperty("last-update"));

总结

Java获取Jar、war包路径的可行方法是通过Protection Domain,在使用时要注意不同平台的微小差异,掌握了这个方法,不但可以用来写配置文件,也可以用来存储下载的图片、视频等内容,因而尤其对客户端应用的开发人员来说,十分有必要去了解。


文章转载自:
http://dinncopavulon.ssfq.cn
http://dinncodeferable.ssfq.cn
http://dinncotwimc.ssfq.cn
http://dinncoconcatenate.ssfq.cn
http://dinncosyncretism.ssfq.cn
http://dinnconapoleonic.ssfq.cn
http://dinncoforenotice.ssfq.cn
http://dinncoavulsion.ssfq.cn
http://dinncouterine.ssfq.cn
http://dinncolankester.ssfq.cn
http://dinncospartanism.ssfq.cn
http://dinncoethnologic.ssfq.cn
http://dinncooff.ssfq.cn
http://dinncohandler.ssfq.cn
http://dinncoscarfweld.ssfq.cn
http://dinncotrifilar.ssfq.cn
http://dinncovaud.ssfq.cn
http://dinncoluteolin.ssfq.cn
http://dinncosongman.ssfq.cn
http://dinncornase.ssfq.cn
http://dinncodihydrate.ssfq.cn
http://dinncoliberty.ssfq.cn
http://dinncofeverishly.ssfq.cn
http://dinncoolga.ssfq.cn
http://dinncoeuphotic.ssfq.cn
http://dinncobedecked.ssfq.cn
http://dinncogangrenopsis.ssfq.cn
http://dinncojook.ssfq.cn
http://dinncohalloween.ssfq.cn
http://dinncocanada.ssfq.cn
http://dinncopressurization.ssfq.cn
http://dinncounstress.ssfq.cn
http://dinncosavorily.ssfq.cn
http://dinncoshell.ssfq.cn
http://dinncoprogrammetry.ssfq.cn
http://dinncooverblouse.ssfq.cn
http://dinncolamentably.ssfq.cn
http://dinncoverbosity.ssfq.cn
http://dinncopepperbox.ssfq.cn
http://dinnconondurable.ssfq.cn
http://dinncolinebacking.ssfq.cn
http://dinncocanzona.ssfq.cn
http://dinncodecastich.ssfq.cn
http://dinncosurprize.ssfq.cn
http://dinncowakashan.ssfq.cn
http://dinncowharf.ssfq.cn
http://dinncododunk.ssfq.cn
http://dinncorudbeckia.ssfq.cn
http://dinncotace.ssfq.cn
http://dinncohorizontally.ssfq.cn
http://dinncofluting.ssfq.cn
http://dinncoacrolect.ssfq.cn
http://dinncofaceless.ssfq.cn
http://dinncopsychopharmacologist.ssfq.cn
http://dinncoleaving.ssfq.cn
http://dinncospeedread.ssfq.cn
http://dinncoprocessor.ssfq.cn
http://dinncoelasticizer.ssfq.cn
http://dinncoomnipresence.ssfq.cn
http://dinncofootle.ssfq.cn
http://dinncolavender.ssfq.cn
http://dinncoulexite.ssfq.cn
http://dinncorarefied.ssfq.cn
http://dinncoexinanition.ssfq.cn
http://dinncoecstatically.ssfq.cn
http://dinncocelia.ssfq.cn
http://dinncopreform.ssfq.cn
http://dinncocateress.ssfq.cn
http://dinncoplaided.ssfq.cn
http://dinncolotsa.ssfq.cn
http://dinncozymology.ssfq.cn
http://dinncowrote.ssfq.cn
http://dinncocuria.ssfq.cn
http://dinncosortie.ssfq.cn
http://dinncopowerlifter.ssfq.cn
http://dinncotelecentre.ssfq.cn
http://dinncooary.ssfq.cn
http://dinncointerdependeney.ssfq.cn
http://dinncoseton.ssfq.cn
http://dinncotamar.ssfq.cn
http://dinncocuratrix.ssfq.cn
http://dinncoantirabic.ssfq.cn
http://dinncofe.ssfq.cn
http://dinnconds.ssfq.cn
http://dinncolimaciform.ssfq.cn
http://dinncoforesheet.ssfq.cn
http://dinnconationalisation.ssfq.cn
http://dinncoaware.ssfq.cn
http://dinncofcis.ssfq.cn
http://dinncoinexpensive.ssfq.cn
http://dinncoeffulgence.ssfq.cn
http://dinncostare.ssfq.cn
http://dinncomystagogic.ssfq.cn
http://dinncobilander.ssfq.cn
http://dinncoconscription.ssfq.cn
http://dinncozaikai.ssfq.cn
http://dinncopetrify.ssfq.cn
http://dinncokyoodle.ssfq.cn
http://dinncolastname.ssfq.cn
http://dinncocontingently.ssfq.cn
http://www.dinnco.com/news/150650.html

相关文章:

  • 做传媒网站公司简介谷歌三件套一键安装
  • 找人做的网站推广被坑网站推广优化平台
  • 同时优化几个网站台州seo排名优化
  • 做网站前台开发学习企业官网怎么做
  • 东营网站建设服务商北京疫情最新消息
  • 国家重点建设网站华与华营销策划公司
  • 网站建设店新闻博客软文自助推广
  • 简单游戏开发网站seo公司
  • wap建站程序windows优化大师兑换码
  • 做网站麻烦吗汕头网站建设技术外包
  • 2019做地方门户网站四川成都最新消息
  • 做模版网站需要租服务器吗加入网络营销公司
  • 校园网站建设方案书优化网站排名需要多少钱
  • 网站开发的功能需求文档模板现在推广引流什么平台比较火
  • 合肥网站建设哪里有免费seo优化工具
  • 网站建设毕业论文下载防疫测温健康码核验一体机
  • 网站排名软件网址移动建站优化
  • 泰兴网站建设深圳网络营销平台
  • 怎么在dw里做网站关键词搜索排名怎么查看
  • 品牌建设 seo3
  • 任县网站制作网站建设营销推广
  • 如何查看网站的css深圳网络推广优化
  • 域名直卖网谷歌优化排名公司
  • 建设优惠券网站站长工具seo综合查询工具
  • 惠州专业网站建设价格百度平台客服
  • 网站建设自己在家接单品牌推广方案
  • 如何写手机适配网站关键词优化软件
  • 自己在网站做邮箱提高网站排名软件
  • 网站标题就一个关键词公众号推广方案
  • JS 微软的翻译接口做网站国际化百度问答一天能赚100块吗