即使关闭AudioInputStream也无法删除文件 [英] Cannot delete file even after closing AudioInputStream

查看:117
本文介绍了即使关闭AudioInputStream也无法删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在代码的某一点,我创建了一个音频输入流:

So at one point of my code, I create an 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停止引用文件?

However, when I try to delete the referenced file, I get a FileSystemException error saying that the file is in use by another process. When I comment out the above code, I no longer get the error and am able to delete the file. Is there a way to force the AudioInputStream to stop referencing the file?

编辑:调用delete的代码-但是,我已经测试了上述代码是否完成在调用delete之前执行(仅在前后使用system.out.print来确保代码当前未运行-不知道更好的方法)

The code calling the delete - however, I have tested to see that the above code is finished executing prior to calling the delete (just using a system.out.print before and after to ensure that the code is not currently running - don't know a better way)

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语句中达到您在上面看到的最低限度,并且仍然出现错误,我不是无缘无故地尝试创建流。

Just FYI, I reduced the code I had inside the try statement to the bare minimum that you see above, and I still get the error, I am not trying to create the stream for no reason.

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

I am running windows 7, but I don't have any error deleting the file when I comment out the code. The exception I get is:

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)


推荐答案


当我尝试删除引用的文件时,出现FileSystemException
错误,指出该文件正在由另一个进程使用

when I try to delete the referenced file, I get a FileSystemException error saying that the file is in use by another process

另一个过程可能就是您:)如果 try 子句。简而言之,如果在调用之前抛出异常,则不会调用此 audioInputStream.close();
处理流时,应始终执行以下操作:

That other process is probably you :) Your code won't close the stream in case there is an exception in the try clause. In a nutshell, this audioInputStream.close(); never gets called in case there is an exception thrown before the call. You should always do the following when dealing with streams:

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

}finally{
  // Close your streams here
}

无论尝试,这都能确保流关闭catch 块。

这篇关于即使关闭AudioInputStream也无法删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆