码迷,mamicode.com
首页 > 编程语言 > 详细

java 中文件的读取File、以及相对路径的问题

时间:2018-05-26 13:57:40      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:eclips   电脑   相对路径   getpath   article   extc   如何获取   dir   回顾   

内容转自: https://blog.csdn.net/fk1778770286/article/details/53900636

1 Properties properties = new Properties();
2 InputStream is = DBUtils.class.getResourceAsStream("jdbc.properties");

以下写法,从class根目录查找文件

1 Properties properties= new Properties();
2 InputStream inStream= DBUtil.class.getClassLoader().getResourceAsStream("configjdbc.properties");
3 properties.load(inStream);

 以下写法从当前类所在package下查找文件

1 Properties properties= new Properties();
2 InputStream inStream= DBUtil.class.getResourceAsStream("configjdbc.properties");
3 properties.load(inStream);

 

 

====================以下来自转载=============

 

由java中的数据库配置文件引入代码,想到文件加载的方式和路径问题

  

一、对于java项目中文件的读取

1、使用System 或是 系统的Properties对象

		①直接是使用  String relativelyPath=System.getProperty("user.dir");
 
②使用Properties对象
我们先来遍历一下系统的属性:
Properties properties = System.getProperties();
Enumeration pnames = properties.propertyNames();
while (pnames.hasMoreElements()) {
String pname = (String) pnames.nextElement();
System.out.print(pname + "--------------");
System.out.println(properties.getProperty(pname));
}
 

给大家截个图:

技术分享图片

这是系统的属性,由此其实还是绕到使用 user.dir 属性来取得当前项目的真是路径  

通过 String relativelyPath = properties.getProperty("user.dir"); 取得

我自己的电脑上面的项目 Log4jProj 的真是路径是 :

user.dir--------------D:\Develop\workspace\ws_self\10_ws_eclipse_j2ee_mars\Log4jProj

其实方式①和方式②一个意思,殊途同归

 

2、第二种方式:使用当前类的类加载器进行获取 ClassLoader

首先来回顾一下,如何获取Class字节码实例,三种方式:(比如我的类叫Demo)

① Demo.class

② Class.forName("类的全称")

③ 利用Demo的实例对象,调用对象的getClass()方法获取该对象的Class实例

 

回顾了如何获取Class字节码实例之后,然后再来回顾一下,如何获取ClassLoader对象

① Demo.class.getClassLoader()

② Class.forName("类的全称").getClassLoader()

③ 假设demo为Demo的实例化对象 demo.getClass().getClassLoader()

④ 通过Thread对象的getContextClassLoader() 方法来获取

Thread.currentThread().getContextClassLoader()

 

进入正题:

有了ClassLoader对象之后,我们这么时候通过ClassLoader对象来获取java项目中的文件

首先让大家看下我当前的项目目录结构

技术分享图片

以及实际文件的目录结构

技术分享图片

技术分享图片

 

需求就是,此时Test需要读取 log4j.properties 文件的路径

用到ClassLoader的两个方法,一个是静态的一个非静态的

技术分享图片

技术分享图片

技术分享图片

输出结果:

技术分享图片

记住哦,这里的getSystemResource方法获取的是URL对象,需要调用getPath()方法获取路径

 

1、当只是获取 log4j.properties 文件输入流的时候可以通过以下两种方式

① 依然是使用 ClassLoader, 其中有两个方法,两者一个是静态一个非静态

技术分享图片

技术分享图片

ClassLoader.getSystemResourceAsStream("config/log4j.properties");

Thread.currentThread().getContextClassLoader().getResourceAsStream("config/log4j.properties");

② 先通过File文件包装之后,然后新建一个输入流

File file01 = new File("config/log4j.properties");
System.out.println(file01.getAbsolutePath());

File file02 = new File(properties.getProperty("user.dir") + "/bin/config/log4j.properties");
System.out.println(file02.getAbsolutePath());

//ClassLoader.getSystemResource获取的是URL对象
File file03 = new File(ClassLoader.getSystemResource("config/log4j.properties").getPath());
System.out.println(file03.getAbsolutePath());

其中创建file03 的方式不建议采纳,因为getSystemResource方法如果没获取到文件,则得到的

URL对象为null,此时再调用getPath()就会报错

 

如果有了文件对象就可以直接创建流了,此处不作赘述

 

java 中文件的读取File、以及相对路径的问题

标签:eclips   电脑   相对路径   getpath   article   extc   如何获取   dir   回顾   

原文地址:https://www.cnblogs.com/zhumaoyu/p/9092655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!