gpt4 book ai didi

java - IO异常 : closed failed on FileOutputStream

转载 作者:行者123 更新时间:2023-11-29 09:00:58 26 4
gpt4 key购买 nike

对于我的应用程序,我将文件从源文件夹复制到目标文件夹以进行进一步的图像处理。我是尝试将 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 缓存数据,我认为您需要调用 force()close() FileChannel 之前。

关于java - IO异常 : closed failed on FileOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17473614/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com