如何获取项目src目录下的资源文件

在我们编写Java的应用程序时,可能会用到一些配置文件,如config.properties,我们可以在配置文件中存储一些重要信息,例如服务器的IP地址,用户名和密码等信息。

在本篇文章当中,只是讨论如何获取到资源文件的路径,并不会对资源文件的内容进行读取。

1、资源目录:src目录和bin目录

在我们编写Java代码的过程当中,我们会把有关的资源文件(例如config.properties)放到src目录和src目录下的某个package中。

但有一点值得注意的是,虽然我们在放置资源文件的时候是在src目录下,但是当我们对资源文件进行读取的时候,却是在(与src同级的)bin目录下。

换句话说,当我们添加资源文件的时候,是在src目录下,但是当程序运行的时候,会将src目录下的资源复制到bin目录下,因此程序访问的资源文件是位于bin目录下的。

在进行代码访问之前,让我来做一些准备,准备三个文件。

第一个文件位于com.rk.io下的config.properties文件,其内容如下:

ServerIP=192.168.80.150
UserName=parentdirectory
Password=123456

第二个文件位于com.rk.io.property下的config.properties文件,其内容如下:

ServerIP=192.168.80.100
UserName=currentdirectory
Password=123456

第三个文件位于com.rk.io.property.subdirectory下的config.properties文件,其内容如下:

ServerIP=192.168.80.50
UserName=subdirectory
Password=123456

2、通过java.lang.Class类的getResource方法访问资源

2.1、以"/"为开头的访问路径的方式

2.1.1、访问第一个文件

代码如下:

package com.rk.io.property;

import java.net.URL;

public class PropertyClassParent
{

	public static void main(String[] args)
	{
		URL resourceURL = PropertyClassParent.class.getResource("/com/rk/io/config.properties");
		String fileName = resourceURL.getFile();
		System.out.println(fileName);
	}

}

输出结果:

/D:/rupeng/workspace/RK/bin/com/rk/io/config.properties

2.1.2、访问第二个文件

代码如下:

package com.rk.io.property;

import java.net.URL;

public class PropertyClassCurrent
{
	public static void main(String[] args)
	{
		URL resourceURL = PropertyClassCurrent.class.getResource("/com/rk/io/property/config.properties");
		String fileName = resourceURL.getFile();
		System.out.println(fileName);
	}
}

输出结果:

/D:/rupeng/workspace/RK/bin/com/rk/io/property/config.properties

2.1.3、访问第三个文件

代码如下:

package com.rk.io.property;

import java.net.URL;

public class PropertyClassSub
{

	public static void main(String[] args)
	{
		URL resourceURL = PropertyClassSub.class.getResource("/com/rk/io/property/subdirectory/config.properties");
		String fileName = resourceURL.getFile();
		System.out.println(fileName);
	}
}

输出结果:

/D:/rupeng/workspace/RK/bin/com/rk/io/property/subdirectory/config.properties

2.2、不以"/"开头的访问路径的方式

2.2.1、访问第一个文件

代码如下:

package com.rk.io.property;

import java.net.URL;

public class PropertyClassParent2
{

	public static void main(String[] args)
	{
		URL resourceURL = PropertyClassParent2.class.getResource("../config.properties");
		String fileName = resourceURL.getFile();
		System.out.println(fileName);
	}
}

输出结果:

/D:/rupeng/workspace/RK/bin/com/rk/io/config.properties

2.2.2、访问第二个文件

代码如下:

package com.rk.io.property;

import java.net.URL;

public class PropertyClassParent2
{

	public static void main(String[] args)
	{
		URL resourceURL = PropertyClassParent2.class.getResource("config.properties");
		String fileName = resourceURL.getFile();
		System.out.println(fileName);
	}
}

输出结果:

/D:/rupeng/workspace/RK/bin/com/rk/io/property/config.properties

2.2.3、访问第三个文件

代码如下:

package com.rk.io.property;

import java.net.URL;

public class PropertyClassSub2
{

	public static void main(String[] args)
	{
		URL resourceURL = PropertyClassSub2.class.getResource("subdirectory/config.properties");
		String fileName = resourceURL.getFile();
		System.out.println(fileName);
	}
}

输出结果:

/D:/rupeng/workspace/RK/bin/com/rk/io/property/subdirectory/config.properties

3、通过java.lang.ClassLoader的getResource方法访问资源

3.1、访问第一个文件

代码如下:

package com.rk.io.property;

import java.net.URL;

public class PropertyClassParent
{

	public static void main(String[] args)
	{
		URL resourceURL = PropertyClassParent.class.getClassLoader().getResource("com/rk/io/config.properties");
		String fileName = resourceURL.getFile();
		System.out.println(fileName);
	}
}

输出结果:

/D:/rupeng/workspace/RK/bin/com/rk/io/config.properties

3.2、访问第二个文件

代码如下:

package com.rk.io.property;

import java.net.URL;

public class PropertyClassCurrent
{
	public static void main(String[] args)
	{
		URL resourceURL = PropertyClassCurrent.class.getClassLoader().getResource("com/rk/io/property/config.properties");
		String fileName = resourceURL.getFile();
		System.out.println(fileName);
	}
}

输出结果:

/D:/rupeng/workspace/RK/bin/com/rk/io/property/config.properties

3.3、访问第三个文件

代码如下:

package com.rk.io.property;

import java.net.URL;

public class PropertyClassLoaderSub
{

	public static void main(String[] args)
	{
		URL resourceURL = PropertyClassLoaderSub.class.getClassLoader().getResource("com/rk/io/property/subdirectory/config.properties");
		String fileName = resourceURL.getFile();
		System.out.println(fileName);
	}
}

输出结果:

/D:/rupeng/workspace/RK/bin/com/rk/io/property/subdirectory/config.properties

4、对比java.lang.Class和java.lang.ClassLoader的getResource方法

对于java.lang.Class类的getResource方法,在传入的参数中:

如果只有一个"/config.properties",则表示bin目录下的config.properties文件;

如果输入"/com/rk/io/config.properties",则表示在bin目录下的com/rk/io内的config.propertiesy文件。

如果输入"config.properties",则表示与当前类的.class文件在同一目录当中的config.properties;

如果输入"../config.properties",则表示当前类的.class的父目录中的config.properties文件。

对于java.lang.ClassLoader的getResource方法,其有一段英文描述如下:

Finds the resource with the given name. A resource is some data (images,
audio, text, etc) that can be accessed by class code in a way that is
independent of the location of the code. 

用这个方法,资源文件的位置和class code之间是彼此独立的(a resource is some data that can be accessed by class code in a way that is independent of the location of the code)

时间: 2024-08-24 09:38:02

如何获取项目src目录下的资源文件的相关文章

java获取程序打包成jar包/exe 同级目录下的资源文件

代码: Properties externalResourceFile = new Properties(); String jarpath = System.getProperty("user.dir") + "/config/externalConfig.properties"; try { FileInputStream in = new FileInputStream(jarpath); externalResourceFile.load(in); Syst

Java web 项目读取src或者tomcat下class目录下的xml文件或者properties文件

//生成一个文件对象: File file = new File(getClass().getClassLoader().getResource("test.xml").getPath()); //直接得到一个输入流: InputStream in = getClass().getClassLoader().getResourceAsStream("test.xml"); //在当前线程获取--这个方法不大稳定 //String path = Thread.curr

获取springboot项目static目录的静态资源

在项目开发的时候肯定会遇到这样的场景需求.比如,需要读取项目static目录下的静态资源.但是应该怎么实现这样的需求呢?最近笔者在开发一个springboot项目也是遇到了类似的需求.就是把一些城市信息存放到一个json文件里面,而这个json文件就是存放到项目的static目录中.接下来就看看笔者是怎么拿到这些城市信息数据的. 步骤一:构造一个json文件 [ { "name": "北京", "city":[{"name":

安卓获取Assets目录下的资源

获取Assets目录下的资源 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin: 15

maven 编译部署src/main/java下的资源文件

maven 编译部署src/main/java下的资源文件 maven默认会把src/main/resources下的所有配置文件以及src/main/java下的所有java文件打包或发布到target\classes下面, 但是现实我们可能会在src/main/java下面也放置一些配置文件如hibernate配置文件或mybatis mapper配置文件等, 如果不做一些额外配置,那我们打包后的项目可能找不到这些必须的资源文件,因此在pom.xml中增加类似如下配置: <build> &

Android 如何引用com.android.internal.R目录下的资源

Android 如何引用com.android.internal.R目录下的资源 项目需求 有一个资源跟系统上的一个资源相同,想要引用它:frameworks/base/core/res/res/drawable/ic_text_dot.xml 文件名称:ic_text_dot.xml 文件的具体内容: <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2014 The Androi

android访问asset目录下的资源

android提供了AssetManager来访问asset目录下的资源, 在activity中通过getAssets()获取AssetManager 常用的api如下: 1.列举路径下的资源String[] list(String path) 2.InputStream open(asset目录下的资源路径) 下面是放问asset目录下的图片的代码 package com.example.qunzheng.customerview; import android.app.Activity; i

Android读取assets目录下的资源

1.获取资源的输入流 资源文件 sample.txt 位于 $PROJECT_HOME/assets/ 目录下,可以在 Activity 中通过 Context.getAssets().open(“sample.txt”) 方法获取输入流. 注意:如果资源文件是文本文件则需要考虑文件的编码和换行符.建议使用UTF-8和Unix换行符. 2. WebView 加载assets目录下的html文件 资源文件 sample.html 位于 $PROJECT_HOME/assets/ 目录下,可以通过以

eclipse maven工程中src/main/resources目录下创建的文件夹是包图标的解决方法

如图:在src/main/resources目录下创建的文件夹却以包的图标显示  修改方法: 入下图,按顺序1 ,2,3,4操作,把3处remove,在4处添加**  修改后如下:  然后点击完成后,文件夹图标显示正常了