亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java文件刪除不了File類的delete方法刪不掉文件的原因以及分析

 更新時(shí)間:2024年06月15日 08:52:34   作者:梁程序員  
這篇文章主要介紹了java文件刪除不了File類的delete方法刪不掉文件的原因以及分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

java文件刪除不了File類的delete方法刪不掉文件

File directory = null;
            String path = tempPath  + File.separator + fileName;
            directory = new File(path);
            if(!directory.exists()){
                directory.mkdirs();
            }
            Writer out = null;
            FileOutputStream fos = null;
            //這個(gè)地方對(duì)流的編碼不可或缺,使用main()單獨(dú)調(diào)用時(shí),應(yīng)該可以,但是如果是web請(qǐng)求導(dǎo)出時(shí)導(dǎo)出后word文檔就會(huì)打不開,并且包XML文件錯(cuò)誤。主要是編碼格式不正確,無法解析。
            //out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
            for (int i = 0; i < dataMapList.size(); i++) {
                File outFile = new File(path+File.separator+fileName+System.currentTimeMillis() + ".doc");
                if (!outFile.exists()) {
                    outFile.createNewFile();
                }
                fos = new FileOutputStream(outFile);
                OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
                out = new BufferedWriter(oWriter);
                t.process(dataMapList.get(i), out);
              
            }
                  out.close();
                fos.close();

流雖然在循環(huán)外關(guān)閉了,但是多個(gè)文件會(huì)導(dǎo)致只能刪除最后一個(gè)文件,應(yīng)該將close 放在循環(huán)內(nèi)

java.io.File刪除被占用的文件問題

while (file.exists()) {
    System.gc();
    file.delete();
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論