#Java holding onto a file I want to delete

1 messages · Page 1 of 1 (latest)

cobalt pilot
#

The program seeks to delete all the images in a folder. But the program has displayed these images in an ImageView. The last image displayed in the view is somehow "held open:" by the Image class - presumably. It was read by code like this:
File selectedImageFile = new File( sImageFilePath ); /* we appear to have a file so we will try to load the image with it. */ InputStream imageAsStream; try { //assert selectedImageFile != null; imageAsStream = new FileInputStream(selectedImageFile); } catch (FileNotFoundException e) { printSysOut("Somehow, image file not found"); throw new RuntimeException(e); } imgImageView.setVisible( true ); anImage = new Image( imageAsStream ); imgImageView.setImage( anImage );

So when the program attempts to delete the file using:
`
try {
File fSourceFile = new File(sSourceAbsFilePath);
if (fSourceFile.delete()) {
iManyDeleted++;
} else {
SSController.printSysOut(String.format("FileHelperCleanSource Problem Deleting File: %s", sSourceAbsFilePath) );
}

        } catch (Exception e) {
            //throw new RuntimeException(e);
            return String.format("Error deleting source file: %s", sSourceAbsFilePath);
        }`

Delete fails with a FALSE.

So the program tries this to clear the image out of the view and toss it out, but is still unable to delete the file: It is still locked apparently.

glad walrusBOT
#

<@&987246487241105418> please have a look, thanks.

cobalt pilot
#

/*
make a tiny image just to remove any references
to image files. 1 pixel, all white
*/
public Image generateImage() {
WritableImage img = new WritableImage(1, 1);
PixelWriter pw = img.getPixelWriter();

    Color color = Color.color(1, 1, 1, 1.0);
    pw.setColor(0, 0, color);
    return img ;
}


void ClearImage() {
    /*
        clear the image we are looking at. Don't change anything else
     */
    /*
        Try to close out any open image in the view by trashing the existing image by
        reading the icon image.
     */
    try {
        anImage = new Image(Objects.requireNonNull(getClass().getResourceAsStream("games-icon.png")) );
        printSysOut("Icon read");

    } catch ( Exception e ) {
        printSysOut("Error setting icon");
        printSysOut( e.toString() );
    }

    imgImageView.setVisible( false );
    imgImageView.setImage( generateImage() );
    lblImageName.setText("");
    sbImageListScrollBar.setMax( 0 );
    sbImageListScrollBar.setMin( 0 );
    sbImageListScrollBar.setValue( 0 );

}
#

That code attempts to clear out the view, and make a new tiny image in memory for the view, and then read the icon into the residual variable that might still hold the image. BUt to no avvail. The image file is still locked and cannot be deleted.

#

Maybe the program should "close the stream". Let's look at that.

river crow
cobalt pilot
#

Yep. Interesting how asking for help makes you think more clearly. Closing the image stream after reading the image fixed the problem. Thanks for helping me think clearly.