获取项目src下的目录
来源:互联网 发布:php 能和sql 编辑:程序博客网 时间:2024/06/06 02:10
1、资源目录:src目录和bin目录
1
2
3
ServerIP=192.168.80.150
UserName=parentdirectory
Password=123456
1
2
3
ServerIP=
192.168
.
80.100
UserName=currentdirectory
Password=
123456
1
2
3
ServerIP=192.168.80.50
UserName=subdirectory
Password=123456
2、通过java.lang.Class类的getResource方法访问资源
2.1、以"/"为开头的访问路径的方式
2.1.1、访问第一个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);
}
}
1
/D:/rupeng/workspace/RK/bin/com/rk/io/config.properties
2.1.2、访问第二个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}
}
1
/D:/rupeng/workspace/RK/bin/com/rk/io/property/config.properties
2.1.3、访问第三个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}
}
1
/D:/rupeng/workspace/RK/bin/com/rk/io/property/subdirectory/config.properties
2.2、不以"/"开头的访问路径的方式
2.2.1、访问第一个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}
}
1
/D:/rupeng/workspace/RK/bin/com/rk/io/config.properties
2.2.2、访问第二个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}
}
1
/D:/rupeng/workspace/RK/bin/com/rk/io/property/config.properties
2.2.3、访问第三个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}
}
1
/D:/rupeng/workspace/RK/bin/com/rk/io/property/subdirectory/config.properties
3、通过java.lang.ClassLoader的getResource方法访问资源
3.1、访问第一个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}
}
1
/D:/rupeng/workspace/RK/bin/com/rk/io/config.properties
3.2、访问第二个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}
}
1
/D:/rupeng/workspace/RK/bin/com/rk/io/property/config.properties
3.3、访问第三个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
}
}
1
/D:/rupeng/workspace/RK/bin/com/rk/io/property/subdirectory/config.properties
4、对比java.lang.Class和java.lang.ClassLoader的getResource方法
0 0
- 获取项目src下的目录
- 获取src下目录
- java web项目获取src和WebContent目录下的配置文件
- 关于在Winodws/Linux下Web项目获取src目录下文件路径的方法
- java动态获取src目录下的文件路径
- jsp项目读取src目录下的配置文件
- web项目读写src目录下的.properties文件
- 获取java中src目录的方法
- scala学习-12-scala读取java项目下Src目录下的properties文件
- java读取src目录下的配置文件
- Java读取src目录下的配置文件
- java 读取src目录下的配置文件
- Maven项目中读取src/main/resources目录下的配置文件
- 获取元素下IMG的src
- web项目 读取src下的配置文件
- 获取项目的目录
- Java web 项目读取src或者tomcat下class目录下的xml文件或者properties文件
- myeclipse的package explorer中,项目的src目录下的相同的包名如何合并在一起
- java实现定时任务的三种方法
- iperf – 統計 jitter 以及 packet loss
- 创建POWL的实例
- 配置本地ip
- 武器类 小游戏(可操控回合制伪完整版) 怪兽类未完成
- 获取项目src下的目录
- 第三周第二篇 本月有几天?
- JavaBean
- 游戏角色纯
- 深度学习(九)caffe预测、特征可视化python接口调用
- PAT1025反转链表 (25)
- C++实验三—多分数段函数求值
- JAVA解析XML四种方式详解
- 常用方法(1)------根据图片的url路径获得Bitmap对象