关闭AudioInputStream后无法删除文件

时间:2012-07-03 17:42:41

标签: java delete-file

因此,在我的代码的某一点上,我创建了一个audioinputstream:

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

但是,当我尝试删除引用的文件时,我收到一个FileSystemException错误,指出该文件正由另一个进程使用。 当我注释掉上面的代码时,我不再收到错误,并且能够删除该文件。有没有办法强制AudioInputStream停止引用该文件?

编辑:调用删除的代码 - 但是,我已经测试过,在调用delete之前看到上面的代码已经完成执行(之前和之后只使用了system.out.print,以确保代码当前不是跑步 - 不知道更好的方式)

File f[] = new File(rootPath + File.separator + directoryNames.get(t)).listFiles();
for(File f2 : f)
{
    try {
        Files.delete(Paths.get(f2.getAbsolutePath()));
    } catch (IOException e) {
    e.printStackTrace();    
    }
}

编辑:仅供参考,我将try语句中的代码减少到你上面看到的最低限度,我仍然得到错误,我没有尝试无缘无故地创建流。

编辑:我正在运行Windows 7,但是当我注释掉代码时,我没有删除文件的任何错误。我得到的例外是:

java.nio.file.FileSystemException: C:\Users\Fred\Desktop\test patient\June 24th, 2011\s1.wav: The process cannot access the file because it is being used by another process.

at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:268)
at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
at java.nio.file.Files.delete(Files.java:1071)
at main.closeTab(main.java:349)
at MainButtonActionListener.actionPerformed(main.java:436)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

3 个答案:

答案 0 :(得分:4)

  

当我尝试删除引用的文件时,我得到一个FileSystemException   错误说该文件正由另一个进程使用

其他进程可能就是你:)如果try子句中存在异常,您的代码将无法关闭流。简而言之,如果在调用之前抛出异常,则永远不会调用此audioInputStream.close();。 在处理流时,您应该始终执行以下操作:

Stream stream = null;
try{
  // Instantiate and do something with stream
}catch(...){

}finally{
  // Close your streams here
}

这确保您的流在try catch块的任何位置都会关闭。

答案 1 :(得分:0)

看起来你正在对事件派发线程进行删除。发生音频流的开/关是什么线程?你能确定它们是按顺序发生的(我知道你添加了一些printlns,但我很好奇这里有任何可能的线程问题。)

我在这里编写了一个隔离的代码块,用于打开/关闭文件,然后将其删除。尝试一下,看看它是否适合你(在这里工作没问题)

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioTest
{

    public static void main(String[] args) throws Exception
    {
        File file = new File(args[0]);
        AudioTest test = new AudioTest();
        test.openCloseFile(file);
        file.delete();
    }

    private void openCloseFile(File file) throws IOException, UnsupportedAudioFileException
    {
        AudioInputStream audioInputStream = null;
        try
        {
            audioInputStream = AudioSystem.getAudioInputStream(file);
        }
        finally
        {
            if (audioInputStream != null)
            {
                audioInputStream.close();
            }
        }
    }
}

答案 2 :(得分:0)

我只是想从评论中重新发布这个内容,因此更容易看到,特别是因为在编辑和删除大量文件时它对我来说非常有效:

尝试将System.gc放入try块中!