文件管理 · 2022年8月21日

properties配置文件怎么写|java 中如何写数据库连接字符窜的properties配置文件

❶ java的Properties文件里面可以有怎样灵活的写法

package properties; import java.io.IOException;import java.util.Properties;import java.util.regex.Matcher;import java.util.regex.Pattern; public class PropertiesTool { private static final Pattern PATTERN = Pattern .compile("\\$\\{([^\\}]+)\\}"); public static String get(Properties properties, String key) { String value = properties.getProperty(key); Matcher matcher = PATTERN.matcher(value); StringBuffer buffer = new StringBuffer(); while (matcher.find()) { String matcherKey = matcher.group(1); String matchervalue = properties.getProperty(matcherKey); if (matchervalue != null) { matcher.appendReplacement(buffer, matchervalue); } } matcher.appendTail(buffer); return buffer.toString(); } public static void main(String[] args) throws IOException { Properties properties = new Properties(); properties.load(PropertiesTool.class .getResourceAsStream("test.properties")); System.out.println(get(properties, "root")); System.out.println(get(properties, "file")); }}test.propertiesroot=/afile=${root}/c输出/a/a/c

❷ java配置文件怎么写

参考java.util.Properties对象进行书写,另外可以在网上找一写辅助书写材料。

❸ db.properties配置文件在哪里配置,第一步怎么做

db.properties连接池的用法Properties文件,其实仅仅是键值对配置文件。下面介绍如何读取properties文件以及如何用JDBC连接数据库。 1.[读取配置文件]: 比如你的properties文件叫做mysql.properties,并且放在com.test包下: java.net.URL url = Thread.currentThread().getContextClassLoader().getResource("com/test/mysql.properties"); 注意路径和包名一致,在你的提问中,我们知道是放在classes目录下,则直接 …getResource("mysql.properties"); 即可 Properties p = new Properties(); p.load(url.openStream()); //由URL载入配置文件 这样你就得到一个Properties的实例 2. [读取配置文件信息]: 比如你要读取drivers的值,只需: String theDriver = p.get("drivers"); 根据配置文件,这样你就得到了"org.gjt.mm.mysql.Driver" 3.[利用JDBC连接数据库] 首先你要保证你有MySQL的Jdbc驱动程序,即包含org.gjt.mm.mysql.Driver的jar包,并放在应用的classpath里。你可以搜索一下,很多地方有下的: mysql-connector-java-3.0.15-ga-bin.jar 然后用同样的方法得到url, mysql.user, mysql.password的值,比如分别为 url, user, pass 然后建立连接: Class.forName("com.mysql.jdbc.Driver"); java.sql.Connection conn = java.sql.DriverManager.getConnection(url, user, pass); 如果不出意外,你就得到连接数据库的Connection了,注意用完了别忘了关啊。(conn.close();) 4.[补充说明] properties文件只是一个键-值对的配置文件(而且键和值都是可以自己写的,并没有固定格式,要看程序需求),从它“本身”只能得到键对应的值。具体这个值用来干什么,那是程序的事情。比如logfile和maxconn,我只能猜是用来处理log和建立连接池时标记最大连接数的,具体怎么实现,需要研究它的代码,因为并没有固定写法。(而上面用jdbc建立连接的写法是固定的)。因为你只给了个properties文件,所以我只能写一个jdbc的连接程序,但这并不是连接池的实现。 这里有个连接池的实现,你可以参考下: http://blog.csdn.net/manud/articles/89022.aspx 2 . SID = orcl ServerName = 192.168.0.24 Port = 1521 User = eclass Password = efound 上面是我在oracle下的配置文件。主要就是你连接数据库时要用到的各个参数,写成属性=值的形势,然后建立数据库连接的时候,参数就不是在bean里面设定,而是来读取这里的参数,就是读文件而已。3 .Java数据库连接池的实现 连接池的配置文件–db.properties(放置在classes目录下) 19:52drivers=sun.jdbc.odbc.JdbcOdbcDriverlogfile=c:/dbpool.log.txtCOMDB.url=jdbc:odbc:COMDBCOMDB.user=AdminCOMDB.password=123456COMDB.initconns=5COMDB.maxconns=50COMDB.logintimeout=5COMDB.loglevel=infoattend.url=jdbc:odbc:attendattend.user=userattend.password=1234attend.initconns=5attend.maxconns=50attend.logintimeout=5attend.loglevel=info

❹ java的properties文件怎么写

最常用读取properties文件的方法InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用:InputStream ins = this.getClass().getResourceAsStream("/cn/zhao/properties/testPropertiesPath2.properties");Java中获取路径方法获取路径的一个简单实现反射方式获取properties文件的三种方式 1 反射方式获取properties文件最常用方法以及思考:Java读取properties文件的方法比较多,网上最多的文章是"Java读取properties文件的六种方法",但在Java应用中,最常用还是通过java.lang.Class类的getResourceAsStream(String name) 方法来实现,但我见到众多读取properties文件的代码中,都会这么干:InputStream in = getClass().getResourceAsStream("资源Name");这里面有个问题,就是getClass()调用的时候默认省略了this!我们都知道,this是不能在static(静态)方法或者static块中使用的,原因是static类型的方法或者代码块是属于类本身的,不属于某个对象,而this本身就代表当前对象,而静态方法或者块调用的时候是不用初始化对象的。问题是:假如我不想让某个类有对象,那么我会将此类的默认构造方法设为私有,当然也不会写别的共有的构造方法。并且我这个类是工具类,都是静态的方法和变量,我要在静态块或者静态方法中获取properties文件,这个方法就行不通了。那怎么办呢?其实这个类就不是这么用的,他仅仅是需要获取一个Class对象就可以了,那还不容易啊-- 取所有类的父类Object,用Object.class难道不比你的用你正在写类自身方便安全吗 ?呵呵,下面给出一个例子,以方便交流。 import java.util.Properties; import java.io.InputStream; import java.io.IOException; /** * 读取Properties文件的例子 * File: TestProperties.java * User: leimin * Date: 2008-2-15 18:38:40 */ public final class TestProperties { private static String param1; private static String param2; static { Properties prop = new Properties(); InputStream in = Object. class .getResourceAsStream( "/test.properties" ); try { prop.load(in); param1 = prop.getProperty( "initYears1" ).trim(); param2 = prop.getProperty( "initYears2" ).trim(); } catch (IOException e) { e.printStackTrace(); } } /** * 私有构造方法,不需要创建对象 */ private TestProperties() { } public static String getParam1() { return param1; } public static String getParam2() { return param2; } public static void main(String args[]){ System.out.println(getParam1()); System.out.println(getParam2()); } } 运行结果: 151 152 当然,把Object.class换成int.class照样行,呵呵,大家可以试试。另外,如果是static方法或块中读取Properties文件,还有一种最保险的方法,就是这个类的本身名字来直接获取Class对象,比如本例中可写成TestProperties.class,这样做是最保险的方法2 获取路径的方式:File fileB = new File( this .getClass().getResource( "" ).getPath()); System. out .println( "fileB path: " + fileB); 2.2获取当前类所在的工程名:System. out .println("user.dir path: " + System. getProperty ("user.dir"))<span style="background-color: white;">3 获取路径的一个简单的Java实现</span> /** *获取项目的相对路径下文件的绝对路径 * * @param parentDir *目标文件的父目录,例如说,工程的目录下,有lib与bin和conf目录,那么程序运行于lib or * bin,那么需要的配置文件却是conf里面,则需要找到该配置文件的绝对路径 * @param fileName *文件名 * @return一个绝对路径 */ public static String getPath(String parentDir, String fileName) { String path = null; String userdir = System.getProperty("user.dir"); String userdirName = new File(userdir).getName(); if (userdirName.equalsIgnoreCase("lib") || userdirName.equalsIgnoreCase("bin")) { File newf = new File(userdir); File newp = new File(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; } } return path; } 4 利用反射的方式获取路径:InputStream ips1 = Enumeration . class .getClassLoader() .getResourceAsStream( "cn/zhao/enumStudy/testPropertiesPath1.properties" ); InputStream ips2 = Enumeration . class .getResourceAsStream( "testPropertiesPath1.properties" ); InputStream ips3 = Enumeration . class .getResourceAsStream( "properties/testPropertiesPath2.properties" );

❺ 如何编写log4j.properties文件,配置日志信息

第一步:在工程中加入log4j所使用的jar文件1:项目 > 属性 :弹出项目的属性窗口2:Java构建路径>?库>?添加外部JAR:弹出选择JAR的窗口3:通过选择JAR的窗口,找到log4j-1.2.x.jar,并确认4:回到项目的属性窗口,点击确定第二步:创建log4j.properties文件1:选择欲使用log4j的项目>?右键点击src >?新建>?其他 :弹出选择向导窗口2:在选择向导窗口中,选择常规?> 文件?> 下一步:弹出新建文件的窗口3:在新建文件窗口中,输入文件名log4j.properties ? 完成:创建工作结束第三步:使用log4j记录日志信息import org.apache.log4j.Logger;public class Log4j { public static void main(String[] args) { Logger logger = Logger.getLogger(AccpTeacherLog4j.class.getName());//获取日志记录器,这个记录器将负责控制日志信息 try { logger.debug("设置教员的编号。");//使用Logger对象的debug、info方法输出日志信息 } catch (IllegalArgumentException ex) { logger.info(ex.getMessage());//使用Logger对象的debug、info方法输出日志信息 } }}第四步:编写log4j.properties文件,配置日志信息### 把日志信息输出到控制台 ###log4j.appender.stdout=org.apache.log4j.ConsoleAppender//日志信息将写到控制台log4j.appender.stdout.Target=System.out//信息打印到System.out上 log4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m%n//指定输出格式:显示日期和log信息 ### 把日志信息输出到文件:accp.log ###log4j.appender.file=org.apache.log4j.FileAppender//日志信息将写到文件中 log4j.appender.file.File=accp.log//指定日志输出的文件名 log4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n//指定输出格式:显示日期,日志发生位置和日志信息### 设置优先级别、以及输出源 ###log4j.rootLogger=debug, stdout, file//设置优先级别为debug、日志被输出到多个输出源注:优先级从高到低分别是ERROR、WARN、INFO、DEBUG在此处,如果优先级别设为info,那么使用debug方法打印的日志信息将不被输出

❻ java 中如何写数据库连接字符窜的properties配置文件

单独定义一个文件:DBConfig.propertiesdriver=com.microsoft.jdbc.sqlserver.SQLServerDriverurl=jdbc:microsoft:sqlserver://localhost:1433;databasename=dbNameuser=sapassword=调用这些信息连接来数据库.一般是在类里.ResourceBundle bundle = ResourceBundle.getBundle("DBConfig"); String driver = bundle.getString("driver"); String url = bundle.getString("url"); String user = bundle.getString("user"); String password = bundle.getString("password"); try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { System.out.println(e.getMessage()); } catch (SQLException e) { System.out.println(e.getMessage()); }验证用户登陆其实就是一个查询方法,根据页面取出的内容.做个equals()判断就可以了.

❼ Java中的properties配置文件怎么写,代码

publicstaticvoidmain(String[]args){Propertiesp=newProperties();p.setProperty("id","user1");p.setProperty("password","123456");try{PrintStreamstm=newPrintStream(newFile("e: est.properties"));p.list(stm);}catch(IOExceptione){e.printStackTrace();}}

❽ 怎样在.properties文件中注释

1、打开IDEA,新建一个Web项目,右键点击新建的项目名,选择创建文件目录(Directory),一般properties文件夹命名应为resoures。

❾ springboot application.properties 如何写多个配置文件

springboot application.properties 写多个配置文件的方法:

# 文件编码

banner.charset= UTF-8

# 文件位置

banner.location= classpath:banner.txt

# 日志配置

# 日志配置文件的位置。 例如对于Logback的`:logback.xml`

logging.config=

# %wEx#记录异常时使用的转换字。

logging.exception-conversion-word=

# 日志文件名。 例如`myapp.log`

logging.file=

# 日志级别严重性映射。 例如`logging.level.org.springframework = DEBUG`

logging.level.*=

# 日志文件的位置。 例如`/ var / log

logging.path=

# 用于输出到控制台的Appender模式。 只支持默认的logback设置。

logging.pattern.console=

# 用于输出到文件的Appender模式。 只支持默认的logback设置。

logging.pattern.file=

# 日志级别的Appender模式(默认%5p)。 只支持默认的logback设置。

logging.pattern.level=

#注册日志记录系统的初始化挂钩。

logging.register-shutdown-hook= false

# AOP 切面

# 添加@EnableAspectJAutoProxy。

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

# 设置是否返回到系统区域设置,如果没有找到特定语言环境的文件。

spring.messages.fallback-to-system-locale= true

# REDIS (Redis 配置)

# 连接工厂使用的数据库索引。

spring.redis.database= 0

# Redis服务器主机。

spring.redis.host= localhost

# 登录redis服务器的密码。

spring.redis.password=

# 给定时间池可以分配的最大连接数。 使用负值为无限制。

spring.redis.pool.max-active= 8

# 池中“空闲”连接的最大数量。 使用负值来表示无限数量的空闲连接。

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

# 管理员 (Spring应用程序管理员JMX自动配置)

# 开启应用管理功能。

spring.application.admin.enabled= false

# JMX应用程序名称MBean。

spring.application.admin.jmx-name= org.springframework.boot:type= Admin,name= SpringApplication

# 自动配置

# 自动配置类排除。

spring.autoconfigure.exclude=

# spring 核心配置

# 跳过搜索BeanInfo类。

spring.beaninfo.ignore= true

# spring 缓存配置

# 由底层缓存管理器支持的要创建的缓存名称的逗号分隔列表。

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=

# 缓存类型,默认情况下根据环境自动检测。

spring.cache.type=

# spring配置 (配置文件应用侦听器)

# 配置文件位置。

spring.config.location=

# 配置文件名。

spring.config.name= application

❿ springboot application.properties 写多个配置文件怎么写

springboot application.properties 写多个配置文件的方法:

# 文件编码

banner.charset= UTF-8

# 文件位置

banner.location= classpath:banner.txt

# 日志配置

# 日志配置文件的位置。 例如对于Logback的`classpath:logback.xml`

logging.config=

# %wEx#记录异常时使用的转换字。

logging.exception-conversion-word=

# 日志文件名。 例如`myapp.log`

logging.file=

# 日志级别严重性映射。 例如`logging.level.org.springframework = DEBUG`

logging.level.*=

# 日志文件的位置。 例如`/ var / log

logging.path=

# 用于输出到控制台的Appender模式。 只支持默认的logback设置。

logging.pattern.console=

# 用于输出到文件的Appender模式。 只支持默认的logback设置。

logging.pattern.file=

# 日志级别的Appender模式(默认%5p)。 只支持默认的logback设置。

logging.pattern.level=

#注册日志记录系统的初始化挂钩。

logging.register-shutdown-hook= false

# AOP 切面

# 添加@EnableAspectJAutoProxy。

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

# 设置是否返回到系统区域设置,如果没有找到特定语言环境的文件。

spring.messages.fallback-to-system-locale= true

# REDIS (Redis 配置)

# 连接工厂使用的数据库索引。

spring.redis.database= 0

# Redis服务器主机。

spring.redis.host= localhost

# 登录redis服务器的密码。

spring.redis.password=

# 给定时间池可以分配的最大连接数。 使用负值为无限制。

spring.redis.pool.max-active= 8

# 池中“空闲”连接的最大数量。 使用负值来表示无限数量的空闲连接。

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

# 管理员 (Spring应用程序管理员JMX自动配置)

# 开启应用管理功能。

spring.application.admin.enabled= false

# JMX应用程序名称MBean。

spring.application.admin.jmx-name= org.springframework.boot:type= Admin,name= SpringApplication

# 自动配置

# 自动配置类排除。

spring.autoconfigure.exclude=

# spring 核心配置

# 跳过搜索BeanInfo类。

spring.beaninfo.ignore= true

# spring 缓存配置

# 由底层缓存管理器支持的要创建的缓存名称的逗号分隔列表。

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=

# 缓存类型,默认情况下根据环境自动检测。

spring.cache.type=

# spring配置 (配置文件应用侦听器)

# 配置文件位置。

spring.config.location=

# 配置文件名。

spring.config.name= application