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
|