System.getProperty("user.dir");
System.getProperty("user.dir")+"src\\..."
System.getProperty("user.dir")+"class\\.."
System.getProperty("user.dir")+"image\\..."
System.getProperty("user.dir")+"src\\..."
System.getProperty("user.dir")+"class\\.."
System.getProperty("user.dir")+"image\\..."
String currentPath=f.getCanonicalPath();
getAbsolutePath()
有什么区别?
File f=new File("./images/start.gif");
String path=f.getPath();
在windows下,得到的是:.\images\start.gif;
2.getAbsolutePath():
a.如果构造File时用的是绝对路径,则反回结果同getPath();
b.如果构造File时用空字符串"",返回当前目录的绝对路径。
如:当前目录为E:\myProject,则返回E:\myProject
c.如果构造File时用的是相对路径,则按照系统默认格式把相对路径解析为绝对路径。windows下:上例的方回结果为:
E:\myProject\.\images\start.gif
3.getCanonicalPath()
做两件事:首先:象getAbsolutePath()一样把相对路径解析为绝对路径。
其次,进行规格化,从绝对路径中去除".",".."等多余字符。如:上面结果为:
E:\myProject\images\start.gif
你可以自己写个程序试一下就清楚了。
System.getProperty("user.dir");
得到的是工作目录路径(可在bat文件属性中设定)!在jb中就是项目目录!如果你是用于web发布则这是你想要的!
用request.getRealPath("");或阿application.getRealPath("");
得到web所在的绝对路径!
然后加上相对路径就可以了!