gpt4 book ai didi

Java复制文件扭曲文件

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:42:27 26 4
gpt4 key购买 nike

所以我试图通过这种方式将文件复制到新位置:

FileReader in = new FileReader(strTempPath);
FileWriter out = new FileWriter(destTempPath);

int c;
while ((c = in.read()) != -1){
out.write(c);
}

in.close();
out.close();

这在 99% 的时间里都能正常工作。有时,如果图像相当小,<= 60x80px,复制的图像会全部扭曲。有谁知道这里会发生什么?是这里复制功能的错还是我应该去别处寻找?

谢谢。

最佳答案

不要使用Readers/Writers 来读取二进制数据。使用 nio 包中的 InputStreams/OutputStreamsChannels(见下文)。

示例来自 exampledepot.com :

try {
// Create channel on the source
FileChannel srcChannel = new FileInputStream("srcFilename").getChannel();

// Create channel on the destination
FileChannel dstChannel = new FileOutputStream("dstFilename").getChannel();

// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

// Close the channels
srcChannel.close();
dstChannel.close();
} catch (IOException e) {
}

关于Java复制文件扭曲文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6283401/

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