1. 首页
  2. 综合百科
  3. properties配置文件怎么创建(java配置properties文件)

properties配置文件怎么创建(java配置properties文件)

简介:关于properties配置文件怎么创建(java配置properties文件)的相关疑问,相信很多朋友对此并不是非常清楚,为了帮助大家了解相关知识要点,小编为大家整理出如下讲解内容,希望下面的内容对大家有帮助!
如果有更好的建议或者想看更多关于综合百科技术大全及相关资讯,可以多多关注茶馆百科网。

各位好,很多人还不知道如何创建properties配置文件(Javaconfigurationpropertiesfile)。下面详细解释一下。现在让我们来看看!

如何在1.Java写属性配置文件,代码

公共静态voidmain(String[]args){

Propertiesp=newProperties();

p.setProperty("id","user1");

p.setProperty("password","123456");

尝试{

PrintStreamstm=newPrintStream(新文件("e:\test.properties"));

p.list(STM);

}catch(IOExceptione){

e.printstacktrace();

}

}

2.2.properties文件怎么写?

Instreamin=类名;类;getclassloader();getresourcestream("propertiesname;属性”);

Propertiesprop=newProperties();

正确负载(英寸)

Oracledb_driver=prop.getproperty("Oracledb_driver-properties中的字段");

如何编写3.java的属性文件?

读取属性文件最常用的方法是inputstreamin=getclass()。getresourcestream("资源名称");此方法要求属性文件和当前类位于同一文件夹下。

如果在不同的包中,必须使用:InputStreamins=this.getClass()。GetResourceStream("/cn/Zhao/properties/testpropertiespath2.properties");Java中获取路径的简单实现,通过反射获取属性文件的三种方式,1。通过反思和思考获取属性文件最常见的方法:Java中读取属性文件的方法有很多,网上最流行的文章是《Java读取属性文件的六种方法》,但在Java应用中,最常用的方法是getResourceAsStream(StringName));java.lang.Class类的方法,但我见过很多读取属性文件的代码,都会这么做:inputstreamin=getclass()。getResourceStream("资源名称");这里有个问题,就是调用getClass()的时候默认省略了这个!众所周知,这不能用在静态(static)方法或者静态块中。原因是静态方法或代码块属于类本身,而不是一个对象,而这个本身就代表了当前的对象,而静态方法或代码块在被调用时不需要初始化对象。问题是:如果我不希望一个类有对象,那么我会把这个类的默认构造函数设置为private,当然也不会写其他的常用构造函数。

而我的类是工具类,都是静态的方法和变量。如果我想在静态块或静态方法中获取属性文件,这个方法就行不通。那我该怎么办?其实这个类不是这么用的。它只需要得到一个类对象。那还不容易——取所有类的父类对象,用Object.class不是比你用你写的类更方便更安全吗?呵呵,这里举个例子方便交流。

导入Java.util.properties;导入Java.io.inputstream;导入Java.io.ioexception;publicfinalclass测试属性{privatestaticstringparam1私有静态字符串param2static{Propertiesprop=newProperties();InputStreamin=Object。班级。getResourceAsStream("/test.properties");试试{prop.load(in);param1=prop.getproperty("inityears1")。trim();param2=prop.getproperty("inityears2")。trim();}catch(ioexceptione){e.printstacktrace();}}privatestproperties(){}publicstaticStringgetparam1(){Returnparam1;}公共静态字符串getparam2(){returnparam2;}publicstaticvoidmain(Stringargs[]){system.out.println(getparam1());system.out.println(getparam2());}}运行结果:151152当然把Object.class改成int.class也是可以的呵呵,可以试试。另外,如果是静态方法或者块来读取属性文件,还有一种最安全的方式,就是类本身的名字可以直接获取类对象。比如在这个例子中,可以写成TestProperties.class,这是获取路径最安全的方式:filefileb=newfile(this.getclass()。getresource("")。getpath());系统。出去。println("fileB路径:"+fileB);2.2获取当前类所在项目的名称:system.out.println("user.dirpath:"+system.getproperty("user.dir"))3获取路径的简单Java实现publicstaticstringgetpath(stringparentdir,stringfileName){stringpath=null;stringuserdir=system.getproperty("user.dir");StringuserdirName=新文件(userdir)。getName();if(userdirname.equalsignorecase("lib")||userdirname.equalsignorecase("bin"){Filenewf=newFile(userdir);Filenewp=newFile(newf.getparent());if(fileName.trim()。equals(""){path=newp.getpath()+file.separator+parentdir;}else{path=newp.getpath()+file.separator+parentDir+file.separator+fileName;}}else{if(fileName.trim()。equals(""){path=userdir+file.separator+parentdir;}else{path=userdir+file.separator+parentDir+file.separator+fileName;}}返回路径;}4通过反射获取路径:InputStreamIPS1=enumeration.class.getclassloader()。GetResourceStream("cn/Zhao/enumstudy/testpropertiespath1.properties");InputStreamips2=枚举。班级。getResourceAsStream("testpropertiespath1.properties");InputStreamips3=枚举。班级。getResourceAsStream("properties/testpropertiespath2.properties");。

如何在4.Java写属性配置文件,代码

公共静态voidmain(String[]args){Propertiesp=newProperties();p.setProperty("id","user1");p.setProperty("password","123456");try{PrintStreamSTM=newPrintStream(newFile("e:\test.properties"));p.list(STM);}catch(ioexceptione){e.printstacktrace();}}。

5.如何编写.properties文件以及如何调用它

属性文件的内容是键值对的形式。例如,如果我编写一个名为test.properties的文件,在打开它之后,我可以在其中写入如下内容:name=Tom。

在java类中,Properties类的对象需要是新的,如下所示:

Properties属性=新属性();

接下来,您需要获取test.properties的文件路径:

stringpath=thread.currentthread()。getContextClassLoader()。getResource("test.properties")。getPath();

然后加载文件:

properties.load(新文件输入流(路径));

最后,你可以得到它的属性:

stringname_1=properties.getproperty("name");

这个name_1的值是“TOM”。

(因为涉及到文件流,加载步骤需要trycatch。根据编译器提示自己添加。)

6.JAVa读取配置文件位置。如何编写ReaderProperties类?

包com导入Java.io.fileinputstream;importJava.io.filenotfoundexception;导入Java.io.fileoutputstream;导入Java.io.ioexception;导入Java.util.properties;publicclassconfiguration{privatepropertiespro;私有文件输入流输入文件;私有FileOutputStreamoutputFilepublicConfiguration(){pro=newProperties();}publicConfiguration(stringfilePath){pro=newproperties();Try{//读取属性文件输入文件=新文件输入流(filepath);//加载文件pro.load(inputfile);inputfile.close();}catch(文件未找到异常e){system.out.println("读取属性文件->失败!-原因:文件路径错误或文件不存在”);e.printstacktrace();}catch(ioexceptione){system.out.println("加载文件->失败!");e.printstacktrace();}}publicstringgetvalue(stringkey){if(pro.containskey(key)){stringvalue=pro.getproperty(key);返回值;}else{return}}publicstringGetvalue(stringfilepath,stringkey){try{stringvalue="";inputfile=newfileinputstream(filepath);pro.load(输入文件);inputfile.close();if(pro.contains(key)){value=pro.getproperty(key);返回值;}else{return}}catch(filenotfoundexceptione){e.printstacktrace();返回“”;}catch(ioexceptione){e.printstacktrace();返回“”;}catch(Exceptione){e.printstacktrace();返回“”;}}publicvoidclear(){pro.clear()中的所有键及其值;}publicvoidsetvalue(stringkey,stringvalue){pro.setproperty(key,value);}publicvoidSavefile(StringfileName,Stringdescription){try{outputfile=newfileoutputstream(fileName);pro.store(outputFile,description);outputfile.close();}catch(filenotfoundexceptione){e.printstacktrace();}catch(ioexceptionioe){ioe.printstacktrace();}}publicstaticvoidmain(string[]args){configurationconf=newconfiguration("。\.\webcontent\\web-INF\\JDBC.properties");stringvalue=conf.getvalue("hibernate.dialect");System.out.println(值);}}。

7.如何在SpringBoot应用程序中编写多个配置文件

SpringBootapplication.properties编写多个配置文件的方法:#文件代码banner.charset=UTF-8#文件位置banner.location=classpath:banner.txt#日志配置#日志配置文件的位置。

例如,`classpath:logback.XML`logging.config=#%wex#oflogback是用于记录异常的转换字。logging.exception-conversion-word=#log文件名。

如myapp.log`logging.file=#log级别严重性映射。例如,`logging.level.org.springframework=debug`logging.level.*=#日志文件的位置。

例如`/var/loglogging.path=#Appender模式输出到控制台。仅支持默认的回退设置。

输出到文件的logging.pattern.console=#Appender模式。仅支持默认的回退设置。

日志级别的Logging.pattern.file=#Appender模式(默认为%5p)。仅支持默认的回退设置。

Logging.pattern.level=#注册日志系统的初始化钩子。logging.register-shutdown-hook=false#AOPtangent#add@。

Spring.aop.auto=true#是否创建基于子类(CGLIB)的代理(true)而不是基于标准和Java接口的代理(false)?spring.AOP.proxy-target-class=false#应用上下文初始化器#应用指示符。

Spring.application.index=#应用程序名称。Spring.application.name=#国际化(消息源自动配置)#spring.messages.basename=messages#逗号分隔的基本名称列表,每个名称都位于ResourceBundle约定之后。

#加载的资源包文件缓存过期,以秒为单位。当设置为-1时,包将被永久缓存。

spring.messages.cache-seconds=-1#消息编码。spring.messages.encoding=UTF-8#设置如果没有找到特定于区域设置的文件,是否返回到系统区域设置。

连接工厂使用的#redis(redis配置)#数据库索引。Spring.redis.database=0#Redis服务器主机。

Spring.redis.host=localhost#登录redis服务器的密码。Spring.redis.password=#给定时间池中可以分配的最大连接数。

负值是无限的。spring.redis.pool.max-active=8#池中“空idle”连接的最大数量。

使用负值表示无限制的空空闲连接数。Spring.redis.pool.max-idle=8#在连接池耗尽并引发异常之前,连接分配应该被阻塞的最长时间(以毫秒为单位)。

使用负值可以无限期阻止。Spring.redis.pool.max-wait=-1#定义池中维护的空空闲连接的最小数量。

该设置仅在为正时有效。spring.redis.pool.min-idle=0#redis服务器端口spring.redis.port=6379#redis服务器名称spring.redis.sentinel.master=#spring.redis.sentinel.nodes=#连接超时(毫秒)。

Spring.redis.timeout=0#administrator(由Spring应用管理员JMX自动配置)#打开应用管理功能。spring.application.admin.enabled=false#JMX应用程序名称MBean。

spring.application.admin.JMX-name=org.springframework.boot:type=admin,name=springapplication#autoconfiguration#autoconfigurationclassexclusionspring.autoconfigure.exclude=#springcoreconfiguration#跳过对BeanInfo类的搜索。

spring.beaninfo.ignore=true#springcacheconfiguration#底层缓存管理器支持的要创建的以逗号分隔的缓存名称列表。Spring.cache.cache-names=#用于初始化EhCache的配置文件的位置。

Spring.cache.ehcache.config=#用于创建缓存的规范。有关规范格式的更多详细信息,请查看CacheBuilderSpec。

Spring.cache.guava.spec=#用于初始化Hazelcast的配置文件的位置。spring.cache.hazelcast.config=#用于初始化Infinispan的配置文件的位置。

spring.cache.infinispan.config=#用于初始化缓存管理器的配置文件的位置。Spring.cache.jcache.config=#用于检索符合JSR-107的缓存管理器的CachingProvider实现的完全限定名。

仅当类路径中有多个JSR-107实现时才需要。spring.cache.jcache.provider=#cachetype,默认根据环境自动检测。

spring.cache.type=#springconfiguration(配置文件应用程序侦听器)#配置文件位置。Spring.config.location=#配置文件名。

spring.config.name=applicationspringboot的多配置文件是指系统中有多个配置文件,所以你可以在不同的运行环境中使用不同的配置文件。开始一个项目一般有两种方式:1。在RootApplication中运行main方法。

2.使用命令:mvnspring-boot:run。默认情况下,这两种方法使用application.properties中的配置信息。如果使用spring.profiles.active,则使用指定的配置信息。这种方法一般在产品运行时使用,需要在开发和测试时指定配置文件。

8.阅读的两种方法。属性文件配置

[html]查看纯文本打印?导入Java.io.ioexception;导入Java.io.inputstream;导入Java.io.inputstreamreader;导入Java.util.properties;导入org.slf4j.logger;导入org.SLF4j.loggerfactory;导入org.springframework.core.io.defaultresourceloader;导入org.springframework.core.io.resource;导入org.springframework.core.io.resourceloader;导入org.springframework.util;导入org.springframework.util.propertiespersister;PublicClasspropertyutils{Privatestaticfinalstringdefault_encoding="UTF-8";privatestaticLoggerLogger=Loggerfactory.getLogger(propertiesutils.class);私有静态属性persisterpropertiespersister=new();privatestaticresourceloaderresourceloader=newdefaultresourceloader();publicstaticpropertiesloadproperties(string。

resourcesPaths)抛出ioexception{Propertiesprops=newProperties();for(Stringlocation:resourcespaths){logger.debug("Loadingpropertiesfilefrom:"+location));InputStream为=nulltry{ResourceResource=Resourceloader.getResource(location);is=resource.getinputstream();propertiesPersister.load(props,newInputStreamReader(is,DEFAULT_ENCODING));}catch(ioexceptionex){logger.info("无法从类路径加载属性:"+location+":"+ex.getmessage());}最后{如果(是!=null){is.close();}}}回归道具;}publicstaticStringgetdataval(Stringkey){try{PropertiesProperties=Propertiesutils.loadProperties("/config/config.Properties");if(properties==null)返回“”;返回新字符串((properties.getProperty(key))。getBytes("ISO8859_1"),"utf-8");}catch(Exceptione){e.printstacktrace();返回null}}}[html]查看纯文本打印?[html]查看纯文本打印?。

9.9.properties文件怎么写?

Instreamin=类名;类;getclassloader();getresourcestream("propertiesname;属性”);

Propertiesprop=newProperties();

正确负载(英寸)

Oracledb_driver=prop.getproperty("Oracledb_driver-properties中的字段");

以上解释了如何创建属性配置文件(java配置属性文件)。这篇文章已经分享到这里了。希望能帮到大家。

本文主要介绍了关于properties配置文件怎么创建(java配置properties文件)的相关养殖或种植技术,综合百科栏目还介绍了该行业生产经营方式及经营管理,关注综合百科发展动向,注重系统性、科学性、实用性和先进性,内容全面新颖、重点突出、通俗易懂,全面给您讲解综合百科技术怎么管理的要点,是您综合百科致富的点金石。
以上文章来自互联网,不代表本人立场,如需删除,请注明该网址:http://23.234.50.4:8411/article/231353.html