Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trouble with AudioInputStream not releasing a file hook?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Trouble with AudioInputStream not releasing a file hook?

    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!!


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Trouble with AudioInputStream not releasing a file hook?

    After some googling this appears to be a Java bug. The workaround is to call System.gc after setting the stream to null, like so:

    AudioInputStream audioInputStream = null;
    File f = null;
    try
    {
      f = new File(main.getWavFileName(0, tab));
      audioInputStream = AudioSystem.getAudioInputStream(f);
    }
    catch(Exception e) 
    {
      e.printStackTrace();
    }finally
    {
      if(audioInputStream != null)
      {
        try
        {
          audioInputStream.close();
          audioInputStream = null;
          f = null;
        }catch(IOExcepiton exc)
        {
          exc.printStackTrace();
        }
        System.gc();
      }
    }

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Trouble with AudioInputStream not releasing a file hook?

    fhoward,
    it also could be the JVM synchronization problem. If you close a file in Thread 1 and try to delete or rename it in thread 2 you may run into this synchronization problem. Especially when you "pipe" as an output and try to delete it later your Operating System may play game with you. For example:

    java MyApp > out.txt
    ....
    java DelApp out.txt

    The 1st app writes something in out.txt and the 2nd app read it and delete the file. It works 99% but sometimes the 1% may occur and you can't do about it...since you restart your machine

Similar Threads

  1. Looking for code examples to Hook into Keyboard
    By toddbailey in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 16th, 2012, 06:13 PM
  2. File Input Trouble
    By mael331 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 13th, 2012, 06:38 AM
  3. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 08:41 PM
  4. Trouble Reading Line in File
    By Mandraix in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 4th, 2011, 10:47 PM
  5. How to create a system wide mouse or keyboard hook?
    By Freaky Chris in forum Java Native Interface
    Replies: 17
    Last Post: June 17th, 2009, 01:06 PM