1

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

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 停止引用文件?

编辑:调用删除的代码 - 但是,我已经测试看到上述代码在调用删除之前已完成执行(只是在之前和之后使用 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)
4

3 回答 3

5

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

try另一个过程可能是你:) 如果子句中有异常,你的代码不会关闭流。简而言之,audioInputStream.close();如果在调用之前抛出异常,则永远不会调用它。在处理流时,您应该始终执行以下操作:

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

}finally{
  // Close your streams here
}

这可以确保无论try catch块做什么,您的流都会关闭。

于 2012-07-03T17:53:00.340 回答
0

看起来您正在事件调度线程上执行删除操作。音频流的打开/关闭发生在哪个线程上?你能确定它们是按顺序发生的吗(我知道你添加了一些 println,但我很好奇这里有任何可能的线程问题)。

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

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();
            }
        }
    }
}
于 2012-07-03T18:07:44.253 回答
0

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

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

于 2018-01-13T22:11:03.607 回答