0

I am working on a media player application and I want to delete the decrypted media file which is created in a specific folder after running the encrypted media file as soon as the JVM terminates. I tried using deleteOnExit(); function but after three attempts it doesn't delete the file. Why might this be happening? Below is the code I am using to delete the file

@FXML  MediaView mv;
       MediaPlayer mp;
       Media me;
@FXML Slider volumeslider;
@FXML Slider seekslider;





@Override
public void initialize(URL location, ResourceBundle resources) {
try { 
     String key = "Mary has one cat";
     File inputFile = new File("C:\\Users\\Administrator\\Downloads\\video\\ec.mp4");

    String path=new File("C:\\Users\\Administrator\\Downloads\\video\\dc.mp4").getAbsolutePath();
     try {
         CryptoUtils.decrypt(key, inputFile,  path);

        } catch (CryptoException ex) {

            ex.printStackTrace();
        }


    me=new Media(new File(path).toURI().toString());
    mp=new MediaPlayer(me);
    mv.setMediaPlayer(mp);
    mp.setAutoPlay(true);

    DoubleProperty width= mv.fitWidthProperty();
    DoubleProperty height= mv.fitHeightProperty();
    width.bind(Bindings.selectDouble(mv.sceneProperty(), "width"));
    height.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));
    volumeslider.setValue(mp.getVolume() * 100);
    volumeslider.valueProperty().addListener(new InvalidationListener() {

        @Override
        public void invalidated(Observable observable) {
            mp.setVolume(volumeslider.getValue() / 100);

        }
    });

    mp.currentTimeProperty().addListener(new ChangeListener<Duration>() {

        @Override
        public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
            seekslider.setValue(newValue.toSeconds());
            seekslider.maxProperty().bind(Bindings.createDoubleBinding(
                    () -> mp.getTotalDuration().toSeconds(),
                    mp.totalDurationProperty()));

            File file = new File(path);
             file.deleteOnExit();
             file.delete();
             file = null;


        }

    });

                seekslider.setOnMouseClicked(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            mp.seek(Duration.seconds(seekslider.getValue()));

            mp.setOnEndOfMedia(new Runnable() {    
                public void run() {
                mp.seek(Duration.minutes(1)); 
               }
                 });
        }

    });
        }catch(Exception e) {

}
 mv.setOnMouseClicked(new EventHandler<MouseEvent>(){

    @Override
    public void handle(MouseEvent event) {
        mp.play();
        mp.pause();

    }

 });

}

public void play(ActionEvent event) {
    mp.play();
    //mp.setRate(1);
    mp.pause();

}
XYZ
  • 15
  • 1
  • 6

2 Answers2

3

On Linux, this test program is reliably deleting the file "test" in the current directory. (For me. On my machine. Fedora Linux. Java 8.)

import java.io.File;
public class Test {
    public static void main(String[] args) {
        File file = new File("test");
        file.deleteOnExit();
    }
}

On Linux, the only things I can think of that would stop this from working would be:

  • if the current user did not have permission to delete the file,
  • if the current directory was on a read-only file system, or
  • if the JVM crashed, preventing the "delete on exit" mechanism from working.

On Windows, you also have to consider the possibility that the "test" file has been opened / locked by another application. (Or potentially by this application, though not in this case.)

Note that deleteOnExit won't throw an exception if the file cannot be deleted, and neither will File.delete(). However, java.nio.file.Files.delete(Path) will throw a nice warm exception if the deletion fails for some reason. That may give you some clues as to what is actually happening.

If this doesn't help, you need to provide a proper self-contained MCVE that other people can try out on their (Windows) machines.


Based on your updated code, it looks like the problem is that the MediaPlayer class has the media file open. Try calling mp.dispose() before the application exits.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

File.deleteOnExit() will just create a shutdown hook via Runtime.addShutdownHook().

There is no sense to use file.deleteOnExit() and after that file.delete()... deleteOnExit will delete the file when virtual machines terminates. Also there are possible problems when use shutdown hook:

1.If the JVM crashes due to some internal error, then it might crash down without having a chance to execute a single instruction.

2.Once started, Shutdown Hooks can be forcibly stopped before completion. If the process does not terminate within some time (there is limit), then the O/S terminates.

xyz
  • 812
  • 9
  • 18