【java读取properties配置文件[转]】教程文章相关的互联网学习教程文章

java 读取properties文件【代码】【图】

一、文件位置properties文件内容:driver=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/qqonline user=root password= 二、代码需要注意的是java工程的路径为src\config.properties,java web的路径为WEB-INF\classes\config.properties 。部署的时候tomcat会将config.properties编译到WEB-INF\classes\config.properties目录下。所以如果在java web工程下仍然使用java工程的路径会无法获取到参数。publicclass DBUt...

Java.util.Properties

Java.util.Properties:一个可以将键值进行持久化存储的对象。Map--Hashtable的子类。Map |--Hashtable |--Properties:用于属性配置文件,键和值都是字符串类型。特点:1:可以持久化存储数据。2:键值都是字符串。3:一般用于配置文件。 |-- load():将流中的数据加载进集合。原理:其实就是将读取流和指定文件相关联,并读取一行数据,因为数据是规则的key=value,所以获取一行后,通过 = 对该行数据进行切割...

Failed to bind properties under 'spring.datasource' to javax.sql.DataSource【代码】

这是我的配置文件 # 国际化配置文件(包名.基础名) spring.messages.basename=i18n.login server.tomcat.uri-encoding=UTF-8 spring.mvc.date-format=yyyy-MM-dd # 禁用缓存 spring.thymeleaf.cache=false #ctrl+f9 重新编译页面 spring.datasource.password=root spring.datasource.username=root spring.datasource.url=jdbc:mysql://localhost:3306/jdbc spring.datasource.driver-class-name=com.alibaba.druid.proxy.Druid...

代码摘抄:class Properties, package java.util , jt.jar【代码】

测试用例String loc="contextPath/test.properties"; InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(loc); Properties info=new Properties(); info.load(is);// you can get the value in properties-file content "key1=value1" String data=info.getProperty("key1"); 源码文件package java.util;import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import jav...

java读取properties配置文件[转]【代码】

网上文章常见的几种读取.properties文件的方式1、使用java.util.Properties类的load()方法 示例:InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); 2、使用java.util.ResourceBundle类的getBundle()方法 ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); 用ResourceBundle读取.properties文件可避免路径问题 ...

JAVA学习第五十三课 — IO流(七)File对象练习 & Properties集合【代码】

一、练习深度遍历文件夹深度遍历很自然而然想到递归,而递归就很自然的想到其实现的底层算法是栈对指定目录下列出所有内容(包含子目录的内容)PS:建议不要遍历C盘import java.io.*;public class Main {public static void main(String[] args) throws IOException {File dir = new File("D:\\ACM集训");ListAllDemo(dir,0);}public static void ListAllDemo(File dir,int level) throws IOException {System.out.println(GetSpac...

Java读写资源文件类Properties【代码】【图】

Java中读写资源文件最重要的类是Properties1) 资源文件要求如下:1、properties文件是一个文本文件2、properties文件的语法有两种,一种是注释,一种属性配置。 注 释:前面加上#号 属性配置:以“键=值”的方式书写一个属性的配置信息。3、properties文件的一个属性配置信息值可以换行,但键不可以换行。值换行用“\”表示。4、properties的属性配置键值前后的空格在解析时候会被忽略。5、properties文件可以只有键而没有值。也...

java代码获取jdbc链接properties【代码】

publicstatic String getDirPath() {Resource resource = null;Properties props = null;String driverClass = null;try {resource = new ClassPathResource("/data.properties");props = PropertiesLoaderUtils.loadProperties(resource);driverClass= (String) props.get("wingPath");} catch (IOException e) {e.printStackTrace();}return driverClass;} 原文:http://www.cnblogs.com/zrui-xyu/p/5045913.html

java – 我可以使用log4j 1.x和log4j.properties文件异步运行我的日志吗?

我目前正致力于“log4j 1.x”与“logback”的表现,即(slf4j). 我可以将异步附加到我的logback,但我找不到任何可以异步我的log4j的链接. 异步只在log4j 2.x中引入?或者有什么办法让我的log4j 1.x工作异步. 请帮助我 谢谢.解决方法:异步日志记录是Log4j 2的优势之一.而不是AsyncAppender,使用Async Loggers:性能要好得多. Log4j 2 performance page比较Log4j-1.x,Logback和Log4j 2.看看然后决定使用什么日志框架. 顺便说一句,Log4j...

java 通过 Properties 读取数据库配置 .properties 文件的使用。【代码】

user=root password=root jdbcUrl=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8; driverClass=com.mysql.jdbc.Driver 调用方法: public void getProperties(){Properties prop = new Properties();try {prop.load(new BufferedInputStream(new FileInputStream(this.getClass().getResource("/").getPath().substring(1)+"system.properties")));System.out.println(prop.getProperty("user"))...

MySQL Connector/J 6.x jdbc.properties 配置, mysql-connector-java-6.0.4.jar 异常【代码】【图】

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>6.0.4</version> </dependency>解决问题 原来新版的驱动配置有些变化: 1. url连接必须设置时区 ### MySQL Connector/J 5.x (旧版连接) #jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8### MySQL Connector/J 6.x (新版连接) jdbc.url=jdbc:mysql:///test?serverTimezone=UTC&characterEncod...

java.io.FileNotFoundException: Could not open ServletContext resource [/db.properties]

Maven项目,application-context.xml、db.properties文件均放置在src/main/resources目录下,Tomcat部署项目,src/main/resources目录下的配置文件默认位置为: {项目名}/WEB-INF/classes,而Spring却在项目根目录下寻找,肯定找不到,因此,配置时指定classpath目录下寻找即可。解决方案如下:  在applicationContext,xml文件下配置  <context:property-placeholder location="classpath:db.properties" />java.io.FileNotFo...

JDBC java数据连接 读取properties 数据库连接池 预编译【代码】

class Demo01 {public static void main(String[] args)throws ClassNotFoundException, SQLException {//1. 注册驱动 告诉虚拟机使用的是哪个数据库软件Class.forName("com.mysql.jdbc.Driver");//2. 获取连接对象Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/newdb3", "root", "root");System.out.println(conn);//3. 创建SQL执行对象Statement stat = conn.createStatement();//4. 执行SQL语句...

原创: druid配置及解决:Failed to bind properties under &#39;spring.datasource&#39; to javax.sql.DataSource【代码】

会报错:Failed to bind properties under ‘spring.datasource‘ to javax.sql.DataSource 网上查了下,没有找打相关的报错解决办法,所以在解决问题后,整理到网上,帮助有需要的朋友。 1:添加配置 druid 依赖包 <!-- 引入druid alibaba begin--><!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.23</version>...

Java 读取 .properties 配置文件详解示例【代码】

Java 开发中,几种读取方式: 1、基于ClassLoder读取配置文件 注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。Properties properties = new Properties();// 使用ClassLoader加载properties配置文件生成对应的输入流InputStream in = PropertiesMain.class.getClassLoader().getResourceAsStream("config/config.properties");// 使用properties对象加载输入流properties.load(in);//获取key...