file cannot be deleted even after closing streams

fuyawaka

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. 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. This is an excel file which I am working with. 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. As soon as I pass my file to the workbookFactory.create() method, my file cannot be deleted by the code.

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.

What am I doing wrong here?

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.

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.

Braj

Find the solution here Close Filehandle for Workbook (apache poi).

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

For more info have a look at the overloaded constructors of WorkbookFactory class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

VBA macros "deleted" after closing excel file

From Dev

Why closing an Input Stream closes the associated File Descriptor as well, even the File Descriptor is shared among multiple streams ?

From Dev

Sublime Text: File remains in tab even after deleted

From Dev

Prevent a file from being deleted even after rm -f

From Dev

Ubuntu: Environment variable is deleted after closing session

From Dev

C# How to make application continue running even after application file is deleted

From Dev

Recover Deleted File Seconds After It was Downloaded and Deleted

From Dev

Kill a file after running & closing

From Dev

Closing a ModelPopup after sending a file

From Dev

Kill a file after running & closing

From Dev

termios settings after closing file

From Dev

Error after closing file explorer

From Dev

NodeJS StreamReader reads even file is deleted

From Dev

Element not getting deleted even after I have used even delegation

From Dev

Zip file is still available for download even file is deleted at server folder

From Dev

Gradle leaves java as a process even after closing android studio

From Dev

How to run a python program in the background even after closing the terminal?

From Dev

Hibernate keeping inactive session in oracle db even after closing the session

From Dev

RequiredFieldValidator error message persists even after closing ModalPopupExtender

From Dev

How to run a python program in the background even after closing the terminal?

From Dev

Visual Studio is not stopping even after closing the app in debug mode

From Dev

Is it possible to keep a socket alive even after closing the app?

From Dev

How to run npm start for reactjs even after closing terminal?

From Dev

That TextBox should retain the same value even after closing the form?

From Dev

Child process hangs, even after closing its input

From Dev

Data is showing in room database even after deleted from server

From Dev

How to remove "ubuntu" from boot options even after I deleted?

From Dev

Tkinter window not closing after closed file dialog

From Dev

Read from file after write, before closing

Related Related

  1. 1

    VBA macros "deleted" after closing excel file

  2. 2

    Why closing an Input Stream closes the associated File Descriptor as well, even the File Descriptor is shared among multiple streams ?

  3. 3

    Sublime Text: File remains in tab even after deleted

  4. 4

    Prevent a file from being deleted even after rm -f

  5. 5

    Ubuntu: Environment variable is deleted after closing session

  6. 6

    C# How to make application continue running even after application file is deleted

  7. 7

    Recover Deleted File Seconds After It was Downloaded and Deleted

  8. 8

    Kill a file after running & closing

  9. 9

    Closing a ModelPopup after sending a file

  10. 10

    Kill a file after running & closing

  11. 11

    termios settings after closing file

  12. 12

    Error after closing file explorer

  13. 13

    NodeJS StreamReader reads even file is deleted

  14. 14

    Element not getting deleted even after I have used even delegation

  15. 15

    Zip file is still available for download even file is deleted at server folder

  16. 16

    Gradle leaves java as a process even after closing android studio

  17. 17

    How to run a python program in the background even after closing the terminal?

  18. 18

    Hibernate keeping inactive session in oracle db even after closing the session

  19. 19

    RequiredFieldValidator error message persists even after closing ModalPopupExtender

  20. 20

    How to run a python program in the background even after closing the terminal?

  21. 21

    Visual Studio is not stopping even after closing the app in debug mode

  22. 22

    Is it possible to keep a socket alive even after closing the app?

  23. 23

    How to run npm start for reactjs even after closing terminal?

  24. 24

    That TextBox should retain the same value even after closing the form?

  25. 25

    Child process hangs, even after closing its input

  26. 26

    Data is showing in room database even after deleted from server

  27. 27

    How to remove "ubuntu" from boot options even after I deleted?

  28. 28

    Tkinter window not closing after closed file dialog

  29. 29

    Read from file after write, before closing

HotTag

Archive