So I am trying to copy a file to a new location this way:
FileReader in = new开发者_运维问答 FileReader(strTempPath);
FileWriter out = new FileWriter(destTempPath);
int c;
while ((c = in.read()) != -1){
out.write(c);
}
in.close();
out.close();
Which works fine 99% of the time. Sometimes, if the image is rather small, <= 60x80px, the copied image comes out all distorted. Does anyone know what might be going on here? Is it the fault of the copy function here or should I be looking elsewhere?
Thanks.
Don't use a Readers
/ Writers
to read binary data. Use a InputStreams
/ OutputStreams
or Channels
from the nio package (see below).
Example from 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) {
}
Convenience class for reading character files. http://download.oracle.com/javase/6/docs/api/java/io/FileReader.html
精彩评论