java读取配置文件路径问题

java打印classpath路径

System.out.println(System.getProperty("java.class.path"));//系统的classpaht路径
System.out.println(System.getProperty("user.dir"));//用户的当前路径
 

一.文件读取

1.利用java.util自带的Properties类读取

Properties类的load方法提供了两种读取文件的方式

(1)reader作为参数,以字符流方式读取

Properties properties = new Properties();
try {
    properties.load(new InputStreamReader(new FileInputStream(fileName),"utf-8"));
} catch (IOException e) {
    e.printStackTrace();
}

String url = properties.getProperty("url")


在load参数里面可以用任意的io装饰类去装饰文件输入流,只要最终装饰成字符流即可;InputStreamReader方法有编码参数,若读取含有中文的文件,文本文件默认编码为ANSI(在windows中就是GBK),所以将编码参数设置为GBK;或者我的idea系统设置为utf-8编码,所以只要先将文件转为utf-8编码即可

(2)inputStream作为参数,以字节流方式读取

    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream(fileName));
    } catch (IOException e) {
        e.printStackTrace();
    }

    String url = properties.getProperty("url")

同理,load方法参数可以添加任意的装饰组件


2.利用java.util自带的ResourceBundle类读取

    ResourceBundle bundle = ResourceBundle.getBundle("config");
    String url = bundle.getString("url");

该方法默认读取的是resources文件夹下的以.properties为后缀的文件,代码中的例子即为config.properties

二.文件路径

有几种方式
1.绝对路径,不赘述
2.相对路径

(1)利用System.getProperty方法

    System.getProperty("user.dir")+"/src/main/resources/config.properties"

System.getProperty("user.dir")会定位到项目的根目录,可以得到该工程项目所有文件的相关路径及环境配置信息
(2)利用类装载器

    String fileName = this.getClass().getClassLoader().getResource("config.properties").getPath();
    InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties");

二者都定位到的是编译过后class文件所在同级目录下的配置文件。前者可以获取文件完整路径,然后通过reader字符流读取文件,对应于上述properties类load方法中的(1),后者可以直接作为字节流参数输入load方法对应(2)

(3)默认

    properties.load(new InputStreamReader(new FileInputStream("config.properties")));

默认定位从项目根目录开始,上面例子读取的是项目根目录下的config.properties文件
 

杂记

附上项目内获取绝对路径的方法:(应该是他人总结的 以前笔记本里面发现的,侵删)

1可以在servlet的init方法里

String path = getServletContext().getRealPath("/");

这将获取web项目的全路径

例如 :E:\eclipseM9\workspace\tree\

tree是我web项目的根目录

2.你也可以随时在任意的class里调用

this.getClass().getClassLoader().getResource("/").getPath();

这将获取 到classes目录的全路径

例如 : E:\eclipseM9/workspace/tree/WEB-INF/classes/

这个方法也可以不在web环境里确定路径,比较好用

3.request.getContextPath(); 是在开发Web项目时,经常用到的方法,是为了解决相对路径的问题,可返回站点的根路径。

获得web根的上下文环境

如 /tree

tree是我的web项目的root context

/*jsp 取得当前目录的路径

path=request.getRealPath("");

/*得到jbossWEB发布临时目录 warUrl=.../tmp/deploy/tmp14544test-exp.war/

path=C:\jboss-4.0.5.GA\server\default\tmp\deploy\tmp14544test-exp.war\

String path = (String)request.getContextPath();

在开发项目中经常获取resources下的文件(配置文件及其他各种各样的文件),本文通过java代码获取其文件及输入流;

主要用类加载器的两个重要方法

    InputStream in = getClass().getClassLoader().getResourceAsStream("log4j.properties");// 获取文件的输入流
    URL url = getClass().getClassLoader().getResource("log4j.properties");// 获取文件的URL

一、通过类加载器获取资源文件的文件流,输入流的用途很多,看你自己怎么用都可以,如用在Properties获取配置信息,或者用来读图片,文本等等......例子:读取resources下的log4j配置文件信息;

    @Test
    public void testStream() throws IOException, URISyntaxException {
        // 获取输入流
        InputStream in = getClass().getClassLoader().getResourceAsStream("log4j.properties");
        // 新建配置对象
        Properties p = new Properties();
        // 通过输入流加载
        p.load(in);
        // 读取key
        String log4j = p.getProperty("log4j.rootLogger");
        System.out.println(log4j);
    }

运行后如下:

二、通过类加载器获取资源文件的URL,URL的用法也是更加强大,可用用URL对象干很多,比如生成File对象,等可查看URL的API,例子:读取resources下的log4j生成File对象

    @Test
    public void testStream() throws IOException, URISyntaxException {
        // 获取URL
        URL url = getClass().getClassLoader().getResource("log4j.properties");
        // 通过url获取File的绝对路径
        File f = new File(url.getFile());
        System.out.println("文件的名子是:" + f.getName());
        System.out.println("文件的大小是:" + f.length());
    }

运行后如下:

以上两个方法对于获取resources下的文件很有用途,多多利用,能幻化出多种用途

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值