求助:File.delete()怎么删不掉 对于执行同一批数据,却也是这样,生成的临时文件有时能删掉,有时删不掉。file.close()? java里好像没这个方法。不知道该怎么判定文件是不是还在读取中。但却奇怪同一批数据生成的临时文件,读取也会有快慢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ...... File file = new File(filePath + fileName); if (!file.exists()) { file.createNewFile(); } ...... BufferedInputStream tempStream = BufferedInputStream(new FileInputStream(file));...... tempStream.close();File file = new File(filePath + fileName); if (file.exists()) { file.delete(); }中间是有对文件的读取,都执行了tempStream.close() 为什么还是删不掉呢? Debug下,看看后一个file的没得到, 有可能是文件較大的問題,不確定。還有個方法可以試試:String myDelFile = "/*文件名及路徑*/";Runtime runtime = Runtime.getRuntime(); try{runtime.exec("CMD /C DEL "+myDelFile);}catch(Exception e){} 因为你是放在一下进程里操作的,文件很有可能在被占用,你可以返回file.delete(),看是不是为真,另外你也可以单独测一下delete();也就是在建完文件后,再单独运行一次delete(); File file = new File(filePath + fileName); if (file.exists()) { file.delete(); if (file.exists()) System.out.println("气死我了,临时文件怎么老删不掉"+filePath + fileName); }我当时运行的时候也加了一个判断,运行几次,后台有时输出有时不输出上面的话,真郁闷。 “把删除放到另一个文件中操作”是什么意思?String myDelFile = "/*文件名及路徑*/";Runtime runtime = Runtime.getRuntime(); try{runtime.exec("CMD /C DEL "+myDelFile);}catch(Exception e){}七楼,这段程序我不太明白,能说一下,这段程序执行的意图吗? 搂主,我可以说90%的可能是你用文件之后没有关闭流。因为这个问题我遇见过,也是偶尔删不掉,最后都是查出来有一个地方没有关闭流。象你下面的代码:BufferedInputStream tempStream = BufferedInputStream(new FileInputStream(file)); ...... tempStream.close();我建议改成下面:FileInputStream fileInputStream = new FileInputStream(file); BufferedInputStream tempStream =new BufferedInputStream(fileInputStream );......fileInputStream.close();tempStream.close();这样就能绝对保证流被关闭了。如果还不行,请你一定把file正在用......流没有关 作为解决可能出现的问题去查。相信你能解决。 qingkangxu,我也将程序改成过这种情况,运行时还是出现同样的问题。FileInputStream fileInputStream = new FileInputStream(file); BufferedInputStream tempStream =new BufferedInputStream(fileInputStream ); ...... fileInputStream.close(); tempStream.close(); 我再试试其他的方法,相信会找到答案的。谢谢各位了,端午节要到了,祝大家端午节快乐! ^_^ 看Groovy代码,在Java中是一样的,改改就行了! def forceDeleteFile(File file) { println '\t\tin forceDeleteFile' Boolean result=false def delCount=0 while(!result && delCount++ <10) { println "\t\ttry to delete file "+ file.getName() +" count:"+delCount System.gc() result = file.delete() } return result } 16楼-->正解 if(in!=null) in.close(); if(os!=null){ os.flush(); os.close();} socket与http迷惑 ssh连接access数据库的出现的问题? structs2入门问题 求JAVA程序员试练的一些项目 纪念帖---同时送分啦!!!(新人优先) 如果DAO实现的方法超出了Interface的方法该如何处理? WebService非要有Web服务器运行吗? 再问:有谁会在axis中实现soap消息的加密和解密来实现安全的web service 求助:socket! 请问杭州的 Hibernate 本地sql查询问题 关于ssh的一些想法
if (!file.exists()) {
file.createNewFile();
}
...... BufferedInputStream tempStream = BufferedInputStream(new FileInputStream(file));
......
tempStream.close();File file = new File(filePath + fileName);
if (file.exists()) {
file.delete();
}中间是有对文件的读取,都执行了tempStream.close() 为什么还是删不掉呢?
還有個方法可以試試:String myDelFile = "/*文件名及路徑*/";
Runtime runtime = Runtime.getRuntime();
try{
runtime.exec("CMD /C DEL "+myDelFile);
}catch(Exception e){
}
if (file.exists()) {
file.delete();
if (file.exists())
System.out.println("气死我了,临时文件怎么老删不掉"+filePath + fileName);
}
我当时运行的时候也加了一个判断,运行几次,后台有时输出有时不输出上面的话,真郁闷。
“把删除放到另一个文件中操作”是什么意思?String myDelFile = "/*文件名及路徑*/";
Runtime runtime = Runtime.getRuntime();
try{
runtime.exec("CMD /C DEL "+myDelFile);
}catch(Exception e){
}
七楼,这段程序我不太明白,能说一下,这段程序执行的意图吗?
BufferedInputStream tempStream = BufferedInputStream(new FileInputStream(file));
......
tempStream.close();
我建议改成下面:FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream tempStream =new BufferedInputStream(fileInputStream );
......
fileInputStream.close();
tempStream.close();这样就能绝对保证流被关闭了。
如果还不行,请你一定把file正在用......流没有关 作为解决可能出现的问题去查。
相信你能解决。
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream tempStream =new BufferedInputStream(fileInputStream );
......
fileInputStream.close();
tempStream.close();
我再试试其他的方法,相信会找到答案的。谢谢各位了,端午节要到了,祝大家端午节快乐! ^_^
def forceDeleteFile(File file)
{
println '\t\tin forceDeleteFile'
Boolean result=false
def delCount=0
while(!result && delCount++ <10)
{
println "\t\ttry to delete file "+ file.getName() +" count:"+delCount
System.gc()
result = file.delete()
}
return result
}
in.close();
if(os!=null){
os.flush();
os.close();}