File still in use by Java / How to close file connection

0

Problem:

Codetodemonstratetheproblem:

importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.util.logging.Level;importjava.util.logging.Logger;importjavafx.application.Application;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.control.Label;importjavafx.scene.image.Image;importjavafx.scene.image.ImageView;importjavafx.scene.layout.BorderPane;importjavafx.scene.layout.HBox;importjavafx.stage.Stage;publicclassviewImageFXextendsApplication{privateGroupg;@Overridepublicvoidstart(StageprimaryStage){BorderPanepane=newBorderPane();ButtonbtnOpen=newButton("Open");
        Button btnDelete = new Button("Delete");

        btnOpen.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                g = createImagewithDetails();
                pane.setCenter(g);
            }
        });
        btnDelete.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                g = null;
                Label lbl =  new Label("eliminado");
                pane.setCenter(lbl);
            }
        });        

        HBox menu = new HBox();
        menu.getChildren().addAll(btnOpen, btnDelete);


        pane.setBottom(menu);


        Scene scene = new Scene(pane, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Group createImagewithDetails() {
        ImageView img = new ImageView();
        Group group = new Group();
        final Image image;
        try {
            image = new Image(new FileInputStream(new File("D:\movie_play_red.png")), 150, 0, true, true);
            img.setImage(image);
        } catch (FileNotFoundException ex) {
            System.out.println("Problema no load da Imagem ");
            Logger.getLogger(viewImageFX.class.getName()).log(Level.SEVERE, null, ex);
        }

        Label lblDescricao = new Label("asflsanfa asisoafgsang asiga");
        group.getChildren().addAll(img, lblDescricao);

        return group;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

How can I force the file to stop being used? Is it not supposed, if you put the Group returned to null that the reference to the file is lost and that way you release it?

    
asked by anonymous 02.02.2018 / 13:33

2 answers

2

Use try-with-resources to close InputStream :

try (InputStream imagemStream = new FileInputStream(new File("D:\movie_play_red.png"))) {
    image = new Image(imagemStream, 150, 0, true, true);
    img.setImage(image);
} catch (FileNotFoundException ex) {
    System.out.println("Problema no load da Imagem ");
    Logger.getLogger(CloseImage.class.getName()).log(Level.SEVERE, null, ex);
}  catch (IOException ex) {
    System.out.println("Problema ao fechar a imagem ");
    Logger.getLogger(CloseImage.class.getName()).log(Level.SEVERE, null, ex);
}
    
02.02.2018 / 15:19
2

You should close FileInputStream after use, try to do this:

FileInputStream fis = new FileInputStream(new File("D:\movie_play_red.png"));
image = new Image(fis, 150, 0, true, true);
img.setImage(image);

// Adicione isso
fis.close();
    
02.02.2018 / 15:07