JAVA读取配置文件的方法

目录

  • JAVA读取配置文件的方法

    • 普通java项目
    • WEB项目

JAVA读取配置文件的方法

普通java项目

1、classLoader

//主要通过当前类的加载器加载classpath下的资源文件,局限是classpath下的
//getResourceAsStream的路径相当于${classpath}/ 参数相对于这个路径来的
Properties properties = new Properties();
InputStream in = PaySupportUtils.class.getClassLoader()
                    .getResourceAsStream("pay.properties");
try {
    properties.load(in);
    String url = properties.getProperty("pay.url");
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

//ClassLoader.getResource(), 参数为空是当前class的路径,'/'为classpath根路径
String path = PaySupportUtils.class.getResource("").getPath();
String path2 = PaySupportUtils.class.getResource("/").getPath();

2、通过InputStream进行读取文件

//可以读取任意路径的文件
Properties properties = new Properties();
// 使用InPutStream流读取properties文件
BufferedReader bufferedReader =
    new BufferedReader(new FileReader("E:/config.properties"));
properties.load(bufferedReader);
properties.getProperty(String key);

3、使用ResourceBundle进行读取

//这种读取方式跟使用ClassLoader基本一致,路径也是classpath/
ResourceBundle bundle = ResourceBundle.getBundle("static/pay2");
bundle.getString("pay.userid")

4、使用Spring提供的PropertiesLoaderUtils.loadAllProperties

//跟ClassLoader、ResourceBundle 路径一致都是classpath/为根路径的
//还有一个好处是Spring进行了加强,可以对文件的内容改变进行实时体现,其他的方法不行
Properties ps = PropertiesLoaderUtils.loadAllProperties("pay.properties");
ps.getProperty("pay.money", "10000");

WEB项目

1、采用ServletContext读取放在src和WEB-INF中的配置文件

//跟其他的没有什么区别,不过这个是获取文件的实际路径,然后进行流读取,路径以 / 开头
String path = "/WEB-INF/classes/db1.properties";
InputStream in = this.getServletContext().getResourceAsStream(path);

2、使用与普通java项目一样的获取方式

原文地址:https://www.cnblogs.com/itck/p/10301876.html

时间: 2024-08-07 15:37:05

JAVA读取配置文件的方法的相关文章

转:java读取配置文件的几种方法

转自: http://www.iteye.com/topic/56496 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小总结一下,主要叙述的是spring读取配置文件的方法. 一.读取xml配置文件 (一)新建一个java bean(HelloBean.java) java 代码 package chb.demo.vo;   public class HelloBean {   private String hell

Java读取配置文件的方式

Java读取配置文件的方式-笔记 1       取当前启动目录下的配置文件 一般来讲启动java程序的时候,在启动的目录下会有配置文件 classLoader.getResource("").getFile()  会取到java当前启动项目的目录,然后指定对应的配置文件路径即可比如conf/conf.properties //取当前启动目录的配置文件 String filePath =classLoader.getResource("").getFile()+&q

Java 读取配置文件 Properties

String filePath="src/cn/ac/iscas/pebble/ufe/conf/id.properties"; InputStream in = new BufferedInputStream(new FileInputStream(filePath)); int i = 0; Properties p = new Properties(); p.load(in); p.getProperty(key); 还有一个方式如下: public class ConfigUt

java 读取配置文件工具类 (how to read values from properties file in java)

Java 读取配置文件工具类 使用 java.util.Properties import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesReader { private static Properties prop; static { reload(); } private static void reload() { prop = new

java 读取配置文件

java.util.Properties是对properties这类配置文件的映射.支持key-value类型和xml类型两种. key-value类型的配置文件大略长这样: 复制代码 #测试环境配置:平台路径配置 jstrd_home=D:/TMS2006/webapp/tms2006/WEB-INF/ dbPort = localhost databaseName = myd dbUserName = root 复制代码 #打头的是注释行,Properties会忽略注释.允许只有key没有v

Shell读取配置文件的方法

参考:http://www.cnblogs.com/binbinjx/p/5680214.html 做批量软件安装自动化时,都喜欢用配置文件的方式改变参数,那怎么通过shell读取配置文件的配置呢?参考以上链接,根据易用性依次讨论三种方法: 假设配置文件config的内容如下: #!/bin/bash #configuration ID=123 IP=192.168.3.154 Name=test 1 直接将配置信息加载到session的环境变量中 #source config #echo $I

使用Java读取配置文件

实现起来,相对比较简单,留个备案吧,废话也不多说,请看代码: package com.jd.***.config; import org.junit.*; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * 用Java读取一个配置文件 * * Created by zhanghao10 on 2016/6/5. */ public class TestProperti

java读取配置文件常用的四种方式

配置文件 放置在src下面 obj.properties className=com.store.order.dao.impl.OrderDaoImpl 方式一 @Test public void test1() throws Exception{ //文件放在src下面.eclipse会自动拷贝一份到bin目录下,或者build/classes下面, InputStream is = Class.forName("com.store.test.test").getClassLoade

java中读取配置文件的方法

转自:http://blog.csdn.net/stypace/article/details/38414871 一.使用org.apache.commons.configuration 需要使用的是jar包:commons-collections-3.2.1.jar.commons-configuration-1.10.jar.commons-lang-2.6.jar和commons-logging-1.2.jar. 可以读取的配置文件:xml和properties 1.读取xml文件 [ja