本文链接: https://blog.csdn.net/wh _ 1991 05 25/article/details/9314675
File的这三种方法在api中进行了说明,以程序为例进行说明。
打包测试;
import java.io.File;
import java.io.IOException;
公共类测试文件路径{
publicstaticvoidmain (字符串[ ] args ) {
//todo自动- generated method stub
system.out.println (system.getproperty (' user.dir ' ) );
try {
system.out.println(------默认相对路径:获取路径不同----- ' );
file file1=new file (' .src\ test1.txt );
system.out.println (文件1.getpath );
system.out.println (文件1.getabsolutepath ();
system.out.println (文件1.getcanonicalpath ();
system.out.println(------默认相对路径:获取路径不同----- ' );
filefile=newfile('.test1.txt );
system.out.println (file.getpath );
系统. out.println (file.getabsolutepath ();
系统. out.println (file.getcanonicalpath ();
system.out.println(----默认绝对路径:获取路径相同------);
file file2=new file (d :\ workspace\ test\ test1.txt );
system.out.println (文件2.getpath );
system.out.println (文件2.getabsolutepath ();
system.out.println (file2. getcanonicalpath ();
}catch(ioexceptione ) {
//todo auto-generated catch块
e .打印堆栈跟踪(;
}
}
}
程序的执行结果如下。
f :eclipse workspacetest EJB
-----默认相对路径:获取路径不同-----
.srctest1.txt
f :eclipse workspacetest EJB.srctest1.txt
f :eclipse workspacesrctest1.txt
-----默认相对路径:获取路径不同-----
.test1.txt
f :eclipse workspacetest EJB.test1.txt
f :eclipse workspacetest EJBtest1.txt
-----默认绝对路径:获取路径相同-----
d :workspacetesttest1.txt
d :workspacetesttest1.txt
d :workspacetesttest1.txt
结论:
如果输入绝对路径,则返回的都是绝对路径。
如果输入为相对路径:
getPath ) )返回File结构方法中的路径,不增加也不减少任何东西
返回getAbsolutePath (实际为user.dir getPath )的内容,从顶部开始为f:(eclipseworkspace ) testEJB、f: ) eclipseworkspace
getCanonicalPath ) )返回完整分析标准符号的路径
————————————————
这是CSDN博客“wh_19910525”的原创文章,符合CC 4.0 BY-SA版权协议。 请附上原文来源的链接和本声明。
原文链接: https://blog.csdn.net/wh _ 1991 05 25/article/details/9314675