【23】java中File类的getPath(),getAbsolutePath(),getCanonicalPath()区别

xiaoxiao2021-02-27  309

java中File类的getPath(),getAbsolutePath(),getCanonicalPath()区别

File file = new File(".\\test.txt"); System.out.println(file.getPath()); System.out.println(file.getAbsolutePath()); System.out.println(file.getCanonicalPath());

输出实例:

.\test.txt E:\workspace\Test\.\test.txt E:\workspace\Test\test.txt

getPath():

返回的是定义时的路径,可能是相对路径,也可能是绝对路径,这个取决于定义时用的是相对路径还是绝对路径。如果定义时用的是绝对路径,那么使用getPath()返回的结果跟用getAbsolutePath()返回的结果一样

getAbsolutePath():

返回的是定义时的路径对应的相对路径,但不会处理“.”和“..”的情况

getCanonicalPath():

返回的是规范化的绝对路径,相当于将getAbsolutePath()中的“.”和“..”解析成对应的正确的路径

 

转载请注明原文地址: https://www.6miu.com/read-4290.html

最新回复(0)