So I have the following code in one of my classes.
try { File f = new File(main.getWavFileName(0, tab)); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(f); audioInputStream.close(); audioInputStream = null; f = null; } catch(Exception e) {e.printStackTrace();}
When I comment out this code, I am able to delete the file being accessed by the audioinputstream. However, when I leave this code in, and later try to delete the file with
try { Files.delete(Paths.get(f2.getAbsolutePath())); } catch (IOException e) { e.printStackTrace(); }
I get a FileSystemException error saying "The process cannot access the file because it is being used by another process."
I assume that the first block of code is the problem, since commenting it out gets rid of the FileSystemException error and allows me to delete the file. However, I'm not sure how I can go further to ensure that there are no remaining references to the file I'm trying to delete. I'd also like to be sure to delete the file now and not on exit. I have also ensured that the first block of code has completed running before deleting the file. Any ideas?
Thanks!!