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

Asked

Viewed 70 times

0

Problem:

inserir a descrição da imagem aqui

Code to demonstrate the problem:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class viewImageFX extends Application {
    private Group g;
    @Override
    public void start(Stage primaryStage) {
        BorderPane pane = new BorderPane();
        Button btnOpen = new Button("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? It’s not supposed to, if you put the Group returned to null that the reference to the file is lost and that forms the release?

2 answers

2

You should close Fileinputstream after use, try to do this way:

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

2


Use the Try-with-Resources to close the 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.