3

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

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,则复制的图像会全部变形。有谁知道这里可能会发生什么?是这里的复制功能的错还是我应该在别处寻找?

谢谢。

4

2 回答 2

11

不要使用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) {
}
于 2011-06-08T18:24:44.227 回答
0

读取字符文件的便利类。http://download.oracle.com/javase/6/docs/api/java/io/FileReader.html

于 2011-06-08T18:29:36.067 回答