2

对于我的应用程序,我将文件从 Source 复制到 Destination 文件夹以进行进一步的图像处理。我正在尝试将 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)
4

1 回答 1

0

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

于 2013-07-04T20:34:32.060 回答