即使在关闭流之后也无法删除文件

时间:2014-04-18 20:38:40

标签: java file apache-poi inputstream fileoutputstream

我正在尝试从URL打开文件,将其复制到临时文件,然后在处理工作完成后删除临时文件。但是,我无法删除该文件。我试过关闭所有流,我尝试删除deleteOnExit()方法和Delete()方法。这是我正在使用的excel文件。当我没有使用excel文件,或者更具体地说是我的代码中显示的Workbook对象时,文件删除就好了。只要我将文件传递给workbookFactory.create()方法,代码就无法删除我的文件。

以下是以下代码:

public class URLtoFileWriting {

/**
 * @param args the command line arguments
 * @throws java.io.IOException
 */

static File destinationFile;
public static void getFile() throws IOException {

    try {
        // TODO code application logic here

        URL fileURL = new URL("http://www.testURL.com/testfile.xlsx");
        URLtoFileWriting.destinationFile = File.createTempFile("remotefile",".xlsx");

        try {
            URLConnection URLconnection = fileURL.openConnection();
            InputStream inputStream = URLconnection.getInputStream();
            FileOutputStream fileoutputStream = new FileOutputStream(URLtoFileWriting.destinationFile);
            IOUtils.copy(inputStream, fileoutputStream);

            Workbook wb = WorkbookFactory.create(URLtoFileWriting.destinationFile);

            System.out.println(URLtoFileWriting.destinationFile.getAbsolutePath());
            inputStream.close();
            fileoutputStream.close();
            System.out.println("Deleted testWB?!" + URLtoFileWriting.destinationFile.delete());



        } catch (IOException ex) {
            Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidFormatException ex) {
            Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
        }


    } catch (MalformedURLException ex) {
        Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
    }


}

}

我正在使用的所有导入:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

我怀疑它与workbookFactory.create()方法有关,因为一旦我将文件传递给此方法,删除就变得不可能了。

我在这里做错了什么?

更新。我遇到了一个问题: 您可以将File传递给FileInputStream,然后将此流传递给WorkbookFactory.Create()方法。

更新了以下代码以反映这一点:

  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import org.apache.commons.io.IOUtils;
  import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  import org.apache.poi.ss.usermodel.Workbook;
  import org.apache.poi.ss.usermodel.WorkbookFactory;


 public class URLtoFileWriting {

/**
 * @param args the command line arguments
 * @throws java.io.IOException
 */

static File destinationFile;
public static void getFile() throws IOException {

    try {
        // TODO code application logic here

        URL fileURL = new URL("http://www.testURL.com/testfile.xlsx");
        URLtoFileWriting.destinationFile = File.createTempFile("remotefile",".xlsx");

        try {
            URLConnection URLconnection = fileURL.openConnection();
            InputStream inputStream = URLconnection.getInputStream();
            FileOutputStream fileoutputStream = new FileOutputStream(URLtoFileWriting.destinationFile);
            IOUtils.copy(inputStream, fileoutputStream);

            FileInputStream FIS = new FileInputStream(URLtoFileWriting.destinationFile);
            Workbook wb = WorkbookFactory.create(FIS);
            FIS.close();
            inputStream.close();
            fileoutputStream.close();
            System.out.println(URLtoFileWriting.destinationFile);
            System.out.println(URLtoFileWriting.destinationFile.delete());



        } catch (IOException ex) {
            Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidFormatException ex) {
            Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
        }


    } catch (MalformedURLException ex) {
        Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
    }


}

}

我希望这会有所帮助。

1 个答案:

答案 0 :(得分:1)

在此处找到解决方案Close Filehandle for Workbook (apache poi)

使用NPOIFSFileSystem#close()OPCPackage#close()

有关更多信息,请查看WorkbookFactory类的重载构造函数。