import java.io.File;
File file=new File("bbsupload\\text.txt");
String realpath=file.getAbsolutePath();

解决方案 »

  1.   

    getabsoulutepath()中要带参数呀,通不过编译咯
      

  2.   

    另外,我只想获得目录BBSUPLOAD的绝对路径咯,不是目录下的文件咯
      

  3.   

    import java.io.File;
    File file=new File("bbsupload");//这样可以直接获得目录
    String realpath=file.getAbsolutePath();//这个方法没有参数
      

  4.   

    不行呀,我的BBSUPLOAD是放在
    d:\tomcat\webppas\bbs\bbsupload下的,
    象你上面的话只能返回d:\tomcat\bin\bbsupload
      

  5.   

    你要把beyond_xiru说的写在jsp或servlet里才能得到d:\tomcat\webppas\bbs\bbsupload
      

  6.   

    String realpath=application.getRealPath("");//得到d:\tomcat\webapps\bbsFile file=new File(realpath+File.separator+"bbsupload"+File.separator+"file_name");
      

  7.   

    按照SERVLET的安全性规定,是不能得到其绝对路径的.
      

  8.   

    那我是不是要这样做?我是删除bbsupload目录下的一个文件,文件名以PIC
    来代表。
     ServletContext application = null;
      String realpath=application.getRealPath("");//
    File f=new File(realpath+File.separator+"bbsupload"+File.separator);
    String realpath1=f.getAbsolutePath();
      File f1=new File(realpath1,pic);
      try
      {
        if (f1.exists())
        {
          f1.delete();
        }}
    catch (Exception e)
    {}
      

  9.   

    String realpath=application.getRealPath("");
    File f1=new File("realpath"+File.separator+"bbsupload",pic);
    try{
        if (f1.exists()){
            f1.delete();
        }
    }
    catch (Exception e){}
      

  10.   

    不行呀,说
    String realpath=application.getRealPath("");
    中空指针错误
      

  11.   

    用相对路径吧,先用
    String path = System.getProperty("user.dir"); //=tomcat的路径
    path+你的相对路径
    再删除它
      

  12.   

    奇怪呀,我在JSP页面中,这样用
    String realpath=request.getRealPath("");
      File f=new File(realpath+"\\bbsupload\\","70.gif");
    out.println(realpath);
        if (f.exists())
        {
          f.delete();
        }
    else
    {
    out.println("no exiset");
    }
    可以正确删除文件,而在JAVA中这样用PageContext pageContext = null;    String realpath=pageContext.getServletConfig().getServletContext().getRealPath("");
    File f1=new File(realpath+"\\bbsupload\\","70.gif");
    try{
        if (f1.exists()){
            f1.delete();
        }
    }
    catch (Exception e){}
    却说我在147行发生空指针错误,为什么呢?}
      

  13.   

    147行是
    String realpath=pageContext.getServletConfig().getServletContext这行
      

  14.   

    哈哈,我自己解决了,
    我在JSP页面中,将request传入去,
    在JAVABEAN中以HttpServletRequest request接收,
    之后就可以用request.getabolutepath()拉