不知道我写的这个是否是楼主想要的结果。
import java.io.File;public class test {
public static void main(String[] args) {
File file = new File("test.java");
String absolutPath = file.getAbsolutePath();
String dirPath = absolutPath.substring(0,absolutPath.lastIndexOf("\\"));
System.out.println(dirPath);

}
}

解决方案 »

  1.   

    请使用System.getProperty("user.dir");
      

  2.   

    我试了一下  application.getRealPath("");,编译器提示application无法识别我的问题是这样的:工程文件在test目录下,
    源文件.java在test\src\目录下
    解释文件.class文件在test\bin\目录下.
    程序运行的时候需要读取test\bin\目录下的一个现有的文件abc.txt
    使用System.getProperty("user.dir");后,编译器提示在test\abc.txt文件不存在.
    实际上我要定位到test\bin\abc.txt,所以所以还有各位大哥帮忙啊!
      

  3.   

    String path = this.getServlet().getServletContext().getRealPath();
      

  4.   

    程序运行的时候是Application,不是Applet,所以getServlet()也不能用,试了一下不行
      

  5.   

    System.out.println(new File("xxx").getParent());
      

  6.   

    getAbsolutePath获得abc.txt的绝对路径