简体   繁体   English

关闭流后也无法删除文件

[英]file cannot be deleted even after closing streams

I'm trying to open a file from a URL, copy it to a temporary file, and then delete the temporary file when processing work is done. 我正在尝试从URL打开文件,将其复制到临时文件,然后在完成处理工作后删除该临时文件。 However, I am unable to delete the file. 但是,我无法删除该文件。 I have tried closing all streams, I have tried deleting with both deleteOnExit() method and the Delete() method. 我尝试关闭所有流,尝试使用deleteOnExit()方法和Delete()方法删除。 This is an excel file which I am working with. 这是我正在使用的Excel文件。 When I am not using an excel file, or more particularly the Workbook object as shown in my code below, the file deletes just fine. 当我不使用excel文件,或更具体地说,如下面的代码所示,不使用Workbook对象时,将文件删除就可以了。 As soon as I pass my file to the workbookFactory.create() method, my file cannot be deleted by the code. 将文件传递给workbookFactory.create()方法后,该代码将无法删除我的文件。

Here's the code below: 这是下面的代码:

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);
    }


}

} }

All the imports I am using: 我正在使用的所有导入:

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;

I suspect it has something to do with the workbookFactory.create() method, as deletion becomes impossible once I have passed the file to this method. 我怀疑它与workbookFactory.create()方法有关,因为一旦将文件传递给此方法就无法删除。

What am I doing wrong here? 我在这里做错了什么?

UPDATE. UPDATE。 I have come across a fix: You can pass the File to a FileInputStream, and then pass this stream to the WorkbookFactory.Create() method. 我遇到了一个修复:您可以将File传递给FileInputStream,然后将此流传递给WorkbookFactory.Create()方法。

Updated code below to reflect this: 下面更新了代码以反映这一点:

  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);
    }


}

} }

I hope this helps. 我希望这有帮助。

Find the solution here Close Filehandle for Workbook (apache poi) . 在此处找到解决方案关闭工作簿的文件句柄(apache poi)

Use NPOIFSFileSystem#close() or OPCPackage#close() . 使用NPOIFSFileSystem#close()OPCPackage#close()

For more info have a look at the overloaded constructors of WorkbookFactory class. 有关更多信息,请查看WorkbookFactory类的重载构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM