【问题标题】:IOException: closed failed on FileOutputStreamIOException:在 FileOutputStream 上关闭失败
【发布时间】:2013-07-02 15:43:59
【问题描述】:

对于我的应用程序,我将文件从源文件夹复制到目标文件夹以进行进一步的图像处理。我是 尝试将 jpg 文件从 src 复制到 dst 文件夹。这 功能似乎有效,但我仍然收到此 IOException 错误。 谁能解释一下为什么 close() 会失败?

 public static void copyPicture(String src, String dst) {

   File pic = null;
   File newPic = null;

pic = new File(src);
newPic = new File(dst);

FileChannel srcChannel = null;
FileChannel dstChannel = null;

try {
srcChannel = new FileInputStream(pic).getChannel();
dstChannel = new FileOutputStream(newPic).getChannel();

dstChannel.transferFrom(srcChannel, 0, srcChannel.size());



} catch (IOException e) {
    e.printStackTrace();
} finally {

    try {

    if(dstChannel != null) {
        dstChannel.close();
    }
        srcChannel.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    srcChannel = null;
    dstChannel = null;

    src = null;
    dst = null;
 }

 }

目录:

07-04 21:43:15.330: E/System(28070): Uncaught exception thrown by finalizer
07-04 21:43:15.330: E/System(28070): java.io.IOException: close failed: EIO (I/O error)
07-04 21:43:15.330: E/System(28070): at libcore.io.IoUtils.close(IoUtils.java:41)
07-04 21:43:15.330: E/System(28070): at     java.io.FileOutputStream.close(FileOutputStream.java:139)
07-04 21:43:15.330: E/System(28070):    at  java.io.FileOutputStream.finalize(FileOutputStream.java:153)
07-04 21:43:15.330: E/System(28070):    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:186)
07-04 21:43:15.330: E/System(28070):    at  java.lang.Daemons$FinalizerDaemon.run(Daemons.java:169)
07-04 21:43:15.330: E/System(28070):    at java.lang.Thread.run(Thread.java:856)
07-04 21:43:15.330: E/System(28070): Caused by: libcore.io.ErrnoException: close  failed: EIO (I/O error)
07-04 21:43:15.330: E/System(28070):    at libcore.io.Posix.close(Native Method)
07-04 21:43:15.330: E/System(28070):    at   libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
07-04 21:43:15.330: E/System(28070):    at  libcore.io.IoUtils.close(IoUtils.java:38)

【问题讨论】:

  • 你的班级叫什么名字?我从未使用过 FileChannel,但也许您也需要关闭 Streams。
  • 我已经关闭了? dstChannel.close();

标签: java android sdk fileoutputstream


【解决方案1】:

由于FileChannel缓存数据我认为你需要在close()FileChannel之前调用force()

【讨论】:

  • 我不明白它为什么会起作用,但我当然测试了它,但无济于事:)
  • 我想如果它是空的,你也许可以关闭它,但这是一个很长的机会,因为我自己没有测试它。事实上,我无法重现该问题。我应该说我只将它作为常规 Java 应用程序进行了测试,因为我无法访问我的 Android 环境 ATM。
  • 仍然遇到同样的异常,真的很沮丧 :) 谢谢大家的帮助
猜你喜欢
  • 2017-11-02
  • 2013-09-10
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多