如何取得文件的绝对路径?

lush 2002-01-23 03:23:14
要执行删除文件的操作,但是只知道该文件的相对路径,如何取得他的绝对路径(linux下)
...全文
86 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wujj 2002-01-23
  • 打赏
  • 举报
回复
我试了与实际不同
xmvigour 2002-01-23
  • 打赏
  • 举报
回复
File f=new File("hello.txt");
String path1=f.getCanonicalPath();
skyyoung 2002-01-23
  • 打赏
  • 举报
回复
ServletContext

getRealPath
public java.lang.String getRealPath(java.lang.String path)
Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Parameters:
path - a String specifying a virtual path
Returns:
a String specifying the real path, or null if the translation cannot be performed
cherami 2002-01-23
  • 打赏
  • 举报
回复
这个非常容易啊,看看JDK的文档中File类的方法,好像是getAbsolutePath,你自己查查吧,很少用,有点忘了。

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧