2.1 相对路径的概念

相对路径指的是相对JVM的启动路径。

举个例子:假设有一java源文件Example.java在d盘根目录下。我们进入命令行窗口,进入到d盘根目录下,然后用“javac Example.java”来编译此文件,编译无错后,会在d盘根目录下自动生成”Example.class”文件。我们再调用”java Example”来运行该程。此时我们已经启动了一个jvm,这个jvm是在d盘根目录下被启动的,所以此jvm所加载的程序中File类的相对路径也就是相对这个路径的,即d盘根目录D:\。

搞清了这些,我们可以使用相对路径来创建文件,例如:

File file = new File(“a.tx”);

file.createNewFile();

假设jvm是在”D:\”下启动的,那么a.txt就会生成在D:\a.txt;

2.2 如何通过文件路径创建文件对象

下面的ResourceTest项目中有4个文件,1.properties、2 .properties、3 .properties、4 .properties。

当我们需要使用这4个文件的时候,怎样创建文件对象呢?

编译后,4个文件的路径如下:

ResourceTest /1.properties
ResourceTest / bin\2.properties
ResourceTest / bin\com\ghs\test\3.properties
ResourceTest / bin\com\ghs\test\sub\4.properties

前面我们说过,JAVA中文件路径是相对JVM的启动路径的,对于简单的JAVA项目,其JVM是在项目名称下启动的 ,所以,ResourceTest中4个文件的相对路径分别是:

./1.properties   或者 1.properties
./ bin\2.properties  或者 bin\2.properties
./ bin\com\ghs\test\3.properties   或者 bin\com\ghs\test\3.properties
./ bin\com\ghs\test\sub\4.properties  或者 bin\com\ghs\test\sub\4.properties

附: “.”或”.\”代表当前目录,这个目录也就是jvm启动路径。

public class MainTest {
	public static void main(String[] args) {
		File file1 = new File("./1.properties");
		//File file1 = new File("test1.txt");
		File file2 = new File("./bin/2.properties");
		//File file2 = new File("bin/2.properties");
		File file3 = new File("./bin/com/ghs/test/3.properties");
		//File file3 = new File("bin/com/ghs/test/3.properties");
		File file4 = new File("./bin/com/ghs/test/sub/4.properties");
		//File file4 = new File("bin/com/ghs/test/sub/4.properties");
		try {
			System.out.println(file1.exists()+":"+file1.getCanonicalPath());
			System.out.println(file2.exists()+":"+file2.getCanonicalPath());
			System.out.println(file3.exists()+":"+file3.getCanonicalPath());
			System.out.println(file4.exists()+":"+file4.getCanonicalPath());
		} catch (IOException e) {
			e.printStackTrace();

程序运行结果如下:

true:D:\me\open\open-project\ResourceTest\1.properties
true:D:\me\open\open-project\ResourceTest\bin\2.properties
true:D:\me\open\open-project\ResourceTest\bin\com\ghs\test\3.properties
true:D:\me\open\open-project\ResourceTest\bin\com\ghs\test\sub\4.properties

上面创建文件的方式太过于繁琐,所以一般情况下,对于test2.txt和text3.txt的获取,我们倾向于采取下面的方法:

File file2 = new File(Test.class.getResource("/test2.txt").toURI());

File file2 = new File(Test.class.getResource("test3.txt").toURI());

Tomcat中的情况,如果在tomcat中运行web应用,此时,如果我们在某个类中使用如下代码:

File f = new File(".");

String absolutePath = f.getAbsolutePath();

System.out.println(absolutePath);

那么输出的将是tomcat下的bin目录.我的机器就D:\work\server\jakarta-tomcat-5.0.28\bin\.,由此可以看出tomcat服务器是在bin目录下启动jvm的,其实是在bin目录下的“catalina.bat”文件中启动jvm的。

2.3 当前目录和上级目录

“.”或”.\”代表当前目录,这个目录也就是jvm启动路径。

下面的代码能得到当前完整目录:

File f = new File(".");

String absolutePath = f.getAbsolutePath();

System.out.println(absolutePath);//D:\

在当前目录下建立文件:File f = new File(“.\\test1.txt”);

“..”代表当前目录的上级目录。

在上级目录建立文件:File f = new File(“..\\..\\test1.txt”);

三、getPath()、getAbsolutePath()、getCanonicalPath()的区别

getPath()获取的是新建文件时的路径,例如:

File file1 = new File(".\\test1.txt");通过getPath()获取的是.\\test1.txt

File file = new File("D:\\Text.txt");通过getPath()获取的是D:\\Text.txt

getAbsolutePath()获取的是文件的绝对路径,返回当前目录的路径+构造file时候的路径,例如:

File file1 = new File(".\\test1.txt");通过getAbsolutePath()获取的是D:\workspace\test\.\test1.txt

getCanonicalPath()获取的也是文件的绝对路径,而且把..或者.这样的符号解析出来,例如:File file = new File("..\\src

\\test1.txt");通过getCanonicalPath()获取的是D:\workspace\src\test1.txt

四、获取上级目录

getParent()或者getParentFile();

五、获取资源的路径

Java中取资源时,经常用到Class.getResource()和ClassLoader.getResource(),这里来看看他们在取资源文件时候的路径问题。

1.Class.getResource(String path)

path  不以’/'开头时,默认是从此类所在的包下取资源;path  以’/'开头时,则是从ClassPath根下获取;

什么意思呢?看下面这段代码的输出结果就明白了:

package testpackage;
public class TestMain{
    public static void main(String[] args) {
        System.out.println(TestMain.class.getResource(""));
        System.out.println(TestMain.class.getResource("/"));

输出结果:

file:/E:/workspace/Test/bin/testpackage/

file:/E:/workspace/Test/bin/

上面说到的【path以’/'开头时,则是从ClassPath根下获取】,在这里就是相当于bin目录(Eclipse环境下)。

如果我们想在TestMain.java中分别取到1~3.properties文件,该怎么写路径呢?代码如下:

package testpackage;
public class TestMain{
    public static void main(String[] args) {
        // 当前类(class)所在的包目录
        System.out.println(TestMain.class.getResource(""));
        // class根目录
        System.out.println(TestMain.class.getResource("/"));
        // TestMain.class在<bin>/testpackage包中
        // 2.properties  在<bin>/testpackage包中
        System.out.println(TestMain.class.getResource("2.properties"));
        // TestMain.class在<bin>/testpackage包中
        // 3.properties  在<bin>/testpackage.subpackage包中
        System.out.println(TestMain.class.getResource("subpackage/3.properties"));
        // TestMain.class在<bin>/testpackage包中
        // 1.properties  在bin目录(class根目录)
        System.out.println(TestMain.class.getResource("/1.properties"));
※Class.getResource和Class.getResourceAsStream在使用时,路径选择上是一样的。 

2.Class.getClassLoader().getResource(String path)

path不能以’/'开头时;

path是从ClassPath根下获取;

Class.getClassLoader().getResource(String path)

package testpackage;
public class TestMain{
    public static void main(String[] args) {
        TestMain t= new TestMain();
        System.out.println(t.getClass());
        System.out.println(t.getClass().getClassLoader());
        System.out.println(t.getClass().getClassLoader().getResource(""));
        System.out.println(t.getClass().getClassLoader().getResource("/"));//null

输出结果:

class testpackage.TestMainsun.misc.Launcher$AppClassLoader@1fb8ee3file:/E:/workspace/Test/bin/

从结果来看【TestMain.class.getResource("/") == t.getClass().getClassLoader().getResource("")】

上面同样的目录结构,使用Class.getClassLoader().getResource(String path)可以这么写:

package testpackage;
public class TestMain{
    public static void main(String[] args) {
        TestMain t= new TestMain();
        System.out.println(t.getClass().getClassLoader().getResource(""));
        System.out.println(t.getClass().getClassLoader().getResource("1.properties"));
        System.out.println(t.getClass().getClassLoader().getResource("testpackage/2.properties"));
        System.out.println(t.getClass().getClassLoader().getResource("testpackage/subpackage/3.properties"));
※Class.getClassLoader().getResource和Class.getClassLoader().getResourceAsStream在使用时,路径选择上也是一样的。 
                    通过这篇博客可以深入理解Java中相对路径与绝对路径的概念,同时对Eclipse和tomcat中的相对路径也做介绍,有助于开发。另外,getPath()、getAbsolutePath()、getCanonicalPath()的区别也做了说明。
2. 用相对路径进访问的注意点
1)相对路径的写法首先需要判断相对位置,稍显麻烦,绝对路径用得较多,注意相对路径不以/开头,都以点开头或者不写点。绝对路径以/开头。
相对路径
				
Java ,你可以使用 java.nio.file.Paths 类和 java.nio.file.Path 接口来获取相对路径。 这里是一个示例代码: import java.nio.file.Paths; public class Main { public static void main(String[] args) { // 获取当前工作目录 String cu...
Java相对路径绝对路径用于指定文件或目录的位置。 相对路径是相对于当前工作目录或基础目录的路径。当前工作目录是指Java程序运行时所处的目录。基础目录可以通过System.getProperty("user.dir")方法获取。例如,如果当前工作目录是"/home/user",那么相对路径"./file.txt"表示文件在"/home/user/file.txt"的位置。 绝对路径是完整的路径,从根目录开始指定文件或目录的位置。在Unix/Linux系统,根目录表示为"/",而在Windows系统,根目录表示为"C:\"或者"D:\"等。例如,绝对路径"/home/user/file.txt"表示文件在"/home/user"目录下的位置。 需要注意的是,相对路径绝对路径在不同操作系统上可能有所不同。因此,在编写Java代码时,最好使用相对路径,并避免依赖于特定操作系统的绝对路径
l000panda: 可惜这样的文章是错误的,你的概念理解错误。 JPA已经帮你维护这这个DEPT_ID了, 你这里就不要再多次一举写String deptId了,直接删掉。当你对java对象SysOrg进行实体绑定时,他更新数据库的时候自然会帮你把deptId塞上。楼主的分析时对API不正确的用法。 @JoinColumn(name=dept_id, referencedColumnName=dept_id) 并删除外键String deptId. Redis info详解 monch.cc: avg_ttl的说明误人子弟表情包 数据库驱动程序 夏天的金鱼姬: 厉害了老哥, 我会了, 谢谢 Spring中EmptyResultDataAccessException异常产生的原理及处理方法  Dandelion、開朗: object = jdbcTemplate.queryForObject();这里不传参会报错,传参的话不知道传啥