422,757 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,757 IT Pros & Developers. It's quick & easy.

How to copy an image file.

P: 14
Hello friends,
I want to copy files of extensio .bmp , .png, .tif , etc.

when I execute the following code i get errors in the file copied.

FileReader fr=new FileReader("D:/image.bmp");
BufferedReader br=new BufferedReader(fr);
FileReader fw=new FileWriter("D:/copyImage.bmp");
String s;
Aug 11 '08 #1
Share this Question
Share on Google+
6 Replies

Expert 10K+
P: 11,448
Don't use Readers and Writers for binary files; use InputStreams and OutputStreams
instead. The first pair handles characters and do quite a bit of conversion (UTF-8
etc.) The second pair handles simple bytes and don't do anything with them.

kind regards,

Aug 11 '08 #2

P: 539
You may also try FileChannel class together with FileInputStream and FileOutputStream.
Aug 11 '08 #3

Expert 100+
P: 1,216
Note, by the way, there's nothing special about this being an "image" file. You question was general to any binary file: for example, how to copy .class files.
Aug 11 '08 #4

P: 14
Thanks for your guidance.
My problem for copying images is solved.
I had used this code for doing that.

FileInputStream fis=new FileInputStream(new File("D:/image.bmp"));
FileOutputStream fos=new FilePutputStream(new FIle("D:/copyImage.bmp"));
int c;

But one more problem arises. The Speed is very slow. When I use this to copy more than 1000-1200 files i.e. more than 200mb It takes around 4-5 min.
spending this much time is not feasible for me.

Is there any other way by which copying of files becomes faster.

Aug 12 '08 #5

Expert 100+
P: 1,216
That way will be painfully slow -- you are copying the file directly, one byte at a time. Take a look at the InputStream/OutputStream methods that take an array of bytes. 8K is a good array size -- your code should be 8 thousand times faster!

Sukatoa's suggestion to use file channels is excellent as well. Did you follow his links and read?
Aug 12 '08 #6

P: 1

if the size of the file is odd bytes (suppose 51 bytes), how it will successfully copy using integers (because integer is of 4 bytes)?
Sep 14 '17 #7

Post your reply

Sign in to post your reply or Sign up for a free account.