System.out.println( new File("").getAbsolutePath() );

解决方案 »

  1.   

    File的这四个函数File getAbsoluteFile() 
              Returns the absolute form of this abstract pathname. 
     String getAbsolutePath() 
              Returns the absolute pathname string of this abstract pathname. 
     File getCanonicalFile() 
              Returns the canonical form of this abstract pathname. 
     String getCanonicalPath() 
              Returns the canonical pathname string of this abstract pathname. 
      

  2.   

    两位老大的方法怎么这么简单??我在网上查,居然用到了servlet,原文如下,能解释一下吗?
    ServletContext m_application.getRealPath(path);他的思路大概是这样
    这就需要获取其绝对路径, 我们用java程序来做一个专门来获取绝对路径的javaBean(path_test.java)就可以了。 path_test.java的代码如下:
    package pathtest;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.jsp.PageContext;//导入PageContext类,不要忘了
    public class path_test
    { protected ServletContext m_application;
    private boolean m_denyPhysicalPath;
    public path_test()
    {}
    public final void initialize(PageContext pageContext)
    throws ServletException
    {
    m_application = pageContext.getServletContext();} public String getPhysicalPath(String filePathName, int option)
    throws IOException
    {
    String path = new String();
    String fileName = new String();
    String fileSeparator = new String();
    boolean isPhysical = false;
    fileSeparator=System.getProperty("file.separator");
    if(filePathName == null)
    throw new IllegalArgumentException("There is no specified destination file (1140).");
    if(filePathName.equals(""))
    throw new IllegalArgumentException("There is no specified destination file (1140).");
    if(filePathName.lastIndexOf("\\") >= 0)
    {
    path = filePathName.substring(0, filePathName.lastIndexOf("\\"));
    fileName = filePathName.substring(filePathName.lastIndexOf("\\") + 1);
    }
    if(filePathName.lastIndexOf("/") >= 0)
    {
    path = filePathName.substring(0, filePathName.lastIndexOf("/"));
    fileName = filePathName.substring(filePathName.lastIndexOf("/") + 1);
    }
    path = path.length() != 0 ? path : "/";
    java.io.File physicalPath = new java.io.File(path);
    if(physicalPath.exists())
    isPhysical = true;
    if(option == 0)
    {
    if(isVirtual(path))
    {
    path = m_application.getRealPath(path);
    if(path.endsWith(fileSeparator))
    path = path + fileName;
    else
    path = String.valueOf((new StringBuffer(String.valueOf(path))).append(fileSeparator).append(fileName));
    return path;
    }
    if(isPhysical)
    {
    if(m_denyPhysicalPath)
    throw new IllegalArgumentException("Physical path is denied (1125).");
    else
    return filePathName;
    } else
    {
    throw new IllegalArgumentException("This path does not exist (1135).");
    }
    }
    if(option == 1)
    {
    if(isVirtual(path))
    {
    path = m_application.getRealPath(path);
    if(path.endsWith(fileSeparator))
    path = path + fileName;
    else
    path = String.valueOf((new StringBuffer(String.valueOf(path))).append(fileSeparator).append(fileName));
    return path;
    }
    if(isPhysical)
    throw new IllegalArgumentException("The path is not a virtual path.");
    else
    throw new IllegalArgumentException("This path does not exist (1135).");
    }
    if(option == 2)
    {
    if(isPhysical)
    if(m_denyPhysicalPath)
    throw new IllegalArgumentException("Physical path is denied (1125).");
    else
    return filePathName;
    if(isVirtual(path))
    throw new IllegalArgumentException("The path is not a physical path.");
    else
    throw new IllegalArgumentException("This path does not exist (1135).");
    } else
    {
    return null;
    }}
    private boolean isVirtual(String pathName) //判断是否是虚拟路径
    {
    if(m_application.getRealPath(pathName) != null)
    {
    java.io.File virtualFile = new java.io.File(m_application.getRealPath(pathName));
    return virtualFile.exists();
    } else
    {
    return false;
    }
    }}
      

  3.   

    那只能说写这个程序的人对Servelet很熟,但是对java文档研究不够仔细。
    不过getAbsoluteFile()得到的路径会有些怪,有可能会是
    d:\works\aa\bb\cc\dd\..\..\ee\somefile.txt
    getCanonicalPath()得到的是正常的路径,但是这个方法有可能抛出异常。