java读取配置properties文件_Java中读取Properties配置文件的几种方式

一、Java jdk读取Properties配置文件

1、通过jdk提供的java.util.Properties类

2、通过java.util.ResourceBundle类来读取

代码示例如下:

/**

* 方式一:通过jdk提供的java.util.Properties类。

* 这种方式需要properties文件的绝对路径

*/

public void readProperties() {

ClassLoader cl = PropertiesDemo. class .getClassLoader();

Properties p=new Properties();

InputStream in= null;

try {

// 可根据不同的方式来获取InputStream

//方式一:InputStream is=new FileInputStream(filePath); 这种方式需要properties文件的绝对路径

// in = new BufferedInputStream(new FileInputStream("D:\tanyang\study-demo\example\src\main\java\com\ty\demo\example\properties\test.properties"));

//方式三:通过PropertiesDemo.class.getClassLoader().getResourceAsStream(fileName)

// 这个方式要求文件在classpath下

//in = PropertiesDemo.class.getClassLoader().getResourceAsStream("test.properties");

if (cl != null ) {

in = cl.getResourceAsStream( "test.properties" );

} else {

in = ClassLoader.getSystemResourceAsStream( "test.properties" );

}

p.load(in);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

finally {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

System.out.println(p.getProperty( "test.port" ).trim());

System.out.println(p.getProperty( "test.siteName" ).trim());

}

3、 maven 工程读取配置文件同上!

二、 spring 读取properties文件

1、通过xml方式加载properties文件

我们以Spring实例化dataSource为例,我们一般会在beans.xml文件中进行如下配置:

现在如果我们要改变dataSource,我们就得修改这些源代码,但是我们如果使用properties文件的话,只需要修改那里面的即可,就不管源代码的东西了。那么如何做呢?

Spring中有个标签,可以用来加载properties配置文件,location是配置文件的路径,我们现在在工程目录的src下新建一个conn.properties文件,里面写上上面dataSource的配置:

dataSource=com.mchange.v2.c3p0.ComboPooledDataSource

driverClass=com.mysql.jdbc.Driver

jdbcUrl=jdbc\:mysql\://localhost\:3306/shop

user=root

password=root

现在只需要在beans.xml中做如下修改即可:

标签也可以用下面的标签来代替,标签我们更加熟悉,可读性更强:

classpath:conn.properties

虽然看起来没有上面的简洁,但是更加清晰,建议使用后面的这种。但是这个只限于xml的方式,即在beans.xml中用${key}获取配置文件中的值value。

2、通过注解方式加载properties文件

我们来看一个例子:假如我们要在程序中获取某个文件的绝对路径,我们很自然会想到不能在程序中写死,那么我们也可以写在properties文件中。还是在src目录下新建一个public.properties文件,假设里面写了一条记录:

filePath=E\:\\web\\apache-tomcat-8.0.26\\webapps\\E_shop\\image

如果想在java代码中通过注解来获取这个filePath的话,首先得在beans.xml文件中配置一下注解的方式:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java可以通过Properties类来读取properties配置文件。具体步骤如下: 1. 创建Properties对象 Properties prop = new Properties(); 2. 加载配置文件 prop.load(new FileInputStream("config.properties")); 3. 读取配置文件的属性值 String value = prop.getProperty("key"); 其,key为配置文件的属性名,value为属性值。 需要注意的是,配置文件的路径需要根据实际情况进行修改,可以使用相对路径或绝对路径。另外,如果配置文件存在文字符,需要使用UTF-8编码格式进行读取。 ### 回答2: Java读取properties配置文件的方法很简单,可以借助JavaProperties类实现。 首先,需要引用java.util.Properties类,通过load()方法读取配置文件的数据,返回一个Properties对象。其配置文件可以是任何文件类型,但其结构必须按照.properties格式书写。在读取时,可以使用相对或绝对路径指定配置文件的位置。 读取配置文件的示例代码如下: ``` import java.util.*; import java.io.*; public class ReadProperties { public static void main(String[] args) { Properties props = new Properties(); try { FileInputStream in = new FileInputStream("config.properties"); props.load(in); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } String url = props.getProperty("url"); String user = props.getProperty("user"); String password = props.getProperty("password"); System.out.println("url: " + url); System.out.println("user: " + user); System.out.println("password: " + password); } } ``` 上述代码,首先创建了一个Properties对象,并通过调用load()方法读取配置文件config.properties的数据。然后,通过调用getProperty()方法分别获取了配置文件的url、user和password属性值,并将其输出到控制台。 另外,也可以使用Class.getResourceAsStream()或ClassLoader.getResourceAsStream()方法读取配置文件。示例代码如下: ``` import java.util.*; import java.io.*; public class ReadProperties { public static void main(String[] args) { Properties props = new Properties(); try { InputStream in = ReadProperties.class.getResourceAsStream("/config.properties"); props.load(in); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } String url = props.getProperty("url"); String user = props.getProperty("user"); String password = props.getProperty("password"); System.out.println("url: " + url); System.out.println("user: " + user); System.out.println("password: " + password); } } ``` 参考上述示例代码,读取properties配置文件非常简单,只需要使用JavaProperties类和相关方法即可。值得注意的是,在读取配置文件时,需要注意配置文件的位置及格式,以避免出现读取失败或读取错误的情况。 ### 回答3: properties 配置文件是一种常见的文本格式文件,其结构类似键值对。在 Java 读取 properties 配置文件非常方便,在 Java 的标准库已经提供了相关的 API。本文将介绍 Java 读取 properties 配置文件的方法。 Java 提供了 java.util.Properties 类用于操作 properties 文件。该类实现了 Map 接口,可以通过 key-value 的方式存储和操作属性,在 Java 读取 properties 文件通常需要进行两个步骤: 1.创建 Properties 对象,同时将 properties 文件的内容加载到 Properties 对象。 2.通过 Properties 对象获取流配置值。 读取 Properties 文件,首先需要创建一个 Properties 对象,通常使用 Properties 类的 load 方法来加载 properties 文件,load 方法支持两种方法加载 properties 文件。其一是使用文件路径加载,其二是使用 InputStream 对象加载。 public void load(InputStream inStream) public void load(Reader reader) 对于第一种方式,看一下下面的代码: Properties properties = new Properties(); properties.load(new FileInputStream("/path/to/my.properties")); 对于第二种方式,将 InputStream 对象传递给 Properties 对象,调用 load 方法来读取内容: InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties"); Properties properties = new Properties(); properties.load(inputStream); 完成 Properties 文件的加载后,接下来就可以通过 getProperty() 方法获取到 key 对应的 value 内容了: String value = properties.getProperty("key"); 当然,Properties 对象还提供了很多其他的方法用于操作 properties 文件,例如,store() 方法用于存储 properties 文件, toString() 方法可用于将 Properties 对象转化为字符串等等。 总的来说,Java 读取 properties 配置文件非常简单,通过上述方法,可以轻松得到 properties 文件配置项,完成自己关于 properties 文件的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值