对于执行同一批数据,却也是这样,生成的临时文件有时能删掉,有时删不掉。
file.close()?  java里好像没这个方法。
不知道该怎么判定文件是不是还在读取中。但却奇怪同一批数据生成的临时文件,读取也会有快慢?

解决方案 »

  1.   

    ...... 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()  为什么还是删不掉呢?
      

  2.   

    Debug下,看看后一个file的没得到,
      

  3.   

    有可能是文件較大的問題,不確定。
    還有個方法可以試試:String myDelFile = "/*文件名及路徑*/";
    Runtime runtime = Runtime.getRuntime();   
    try{
    runtime.exec("CMD /C DEL "+myDelFile);
    }catch(Exception e){
    }
      

  4.   

    因为你是放在一下进程里操作的,文件很有可能在被占用,你可以返回file.delete(),看是不是为真,另外你也可以单独测一下delete();也就是在建完文件后,再单独运行一次delete();
      

  5.   

        File file = new File(filePath + fileName);
        if (file.exists()) {
            file.delete();
            if (file.exists())
                 System.out.println("气死我了,临时文件怎么老删不掉"+filePath + fileName);
        }
    我当时运行的时候也加了一个判断,运行几次,后台有时输出有时不输出上面的话,真郁闷。
      

  6.   


    “把删除放到另一个文件中操作”是什么意思?String myDelFile = "/*文件名及路徑*/";
    Runtime runtime = Runtime.getRuntime();   
    try{
    runtime.exec("CMD /C DEL "+myDelFile);
    }catch(Exception e){
    }
    七楼,这段程序我不太明白,能说一下,这段程序执行的意图吗?
      

  7.   

    搂主,我可以说90%的可能是你用文件之后没有关闭流。因为这个问题我遇见过,也是偶尔删不掉,最后都是查出来有一个地方没有关闭流。象你下面的代码:
    BufferedInputStream tempStream = BufferedInputStream(new FileInputStream(file)); 
    ...... 
    tempStream.close();
    我建议改成下面:FileInputStream fileInputStream = new FileInputStream(file); 
    BufferedInputStream tempStream =new BufferedInputStream(fileInputStream );
    ......
    fileInputStream.close();
    tempStream.close();这样就能绝对保证流被关闭了。
    如果还不行,请你一定把file正在用......流没有关  作为解决可能出现的问题去查。
    相信你能解决。
      

  8.   

    qingkangxu,我也将程序改成过这种情况,运行时还是出现同样的问题。
    FileInputStream fileInputStream = new FileInputStream(file); 
    BufferedInputStream tempStream =new BufferedInputStream(fileInputStream ); 
    ...... 
    fileInputStream.close(); 
    tempStream.close(); 
    我再试试其他的方法,相信会找到答案的。谢谢各位了,端午节要到了,祝大家端午节快乐! ^_^
      

  9.   

     看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
    }
      

  10.   

    16楼-->正解   if(in!=null)
    in.close();
    if(os!=null){
    os.flush();
    os.close();}