JAVA删除文件请教 import java.io.File;File file=new File("bbsupload\\text.txt");String realpath=file.getAbsolutePath(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 getabsoulutepath()中要带参数呀,通不过编译咯 另外,我只想获得目录BBSUPLOAD的绝对路径咯,不是目录下的文件咯 import java.io.File;File file=new File("bbsupload");//这样可以直接获得目录String realpath=file.getAbsolutePath();//这个方法没有参数 不行呀,我的BBSUPLOAD是放在d:\tomcat\webppas\bbs\bbsupload下的,象你上面的话只能返回d:\tomcat\bin\bbsupload 你要把beyond_xiru说的写在jsp或servlet里才能得到d:\tomcat\webppas\bbs\bbsupload String realpath=application.getRealPath("");//得到d:\tomcat\webapps\bbsFile file=new File(realpath+File.separator+"bbsupload"+File.separator+"file_name"); 按照SERVLET的安全性规定,是不能得到其绝对路径的. 那我是不是要这样做?我是删除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){} String realpath=application.getRealPath("");File f1=new File("realpath"+File.separator+"bbsupload",pic);try{ if (f1.exists()){ f1.delete(); }}catch (Exception e){} 不行呀,说String realpath=application.getRealPath("");中空指针错误 用相对路径吧,先用String path = System.getProperty("user.dir"); //=tomcat的路径path+你的相对路径再删除它 奇怪呀,我在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行发生空指针错误,为什么呢?} 147行是String realpath=pageContext.getServletConfig().getServletContext这行 哈哈,我自己解决了,我在JSP页面中,将request传入去,在JAVABEAN中以HttpServletRequest request接收,之后就可以用request.getabolutepath()拉 字符串问题 小问题,求真像,谢谢。 关于类型转换的一个问T,,,,,,, JAVA API 中的API 全写是什么? factory mothod模式主要用于设计什么样的classes 为什么进入程序员的门槛会如此艰难?(3) 请教一个线程的问题!!! JTree问题请教? 请问java提高方法 刚过了SCJP,来散分,大家来捧场啊!!! 还是没解决呀!!1 求助:怎样在java类中调用可执行文件(比如:.exe,.sh等等)?尤其是Unix的shell程序
File file=new File("bbsupload");//这样可以直接获得目录
String realpath=file.getAbsolutePath();//这个方法没有参数
d:\tomcat\webppas\bbs\bbsupload下的,
象你上面的话只能返回d:\tomcat\bin\bbsupload
来代表。
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)
{}
File f1=new File("realpath"+File.separator+"bbsupload",pic);
try{
if (f1.exists()){
f1.delete();
}
}
catch (Exception e){}
String realpath=application.getRealPath("");
中空指针错误
String path = System.getProperty("user.dir"); //=tomcat的路径
path+你的相对路径
再删除它
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行发生空指针错误,为什么呢?}
String realpath=pageContext.getServletConfig().getServletContext这行
我在JSP页面中,将request传入去,
在JAVABEAN中以HttpServletRequest request接收,
之后就可以用request.getabolutepath()拉