471,893 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,893 software developers and data experts.

How to copy an image file.

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
6 12160
11,448 Expert 8TB
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
539 512MB
You may also try FileChannel class together with FileInputStream and FileOutputStream.
Aug 11 '08 #3
1,216 Expert 1GB
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
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
1,216 Expert 1GB
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

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.

Similar topics

4 posts views Thread by moondaddy | last post: by
1 post views Thread by friglob | last post: by
3 posts views Thread by shotokan99 | last post: by
7 posts views Thread by =?Utf-8?B?TWlrZQ==?= | last post: by
1 post views Thread by Sinan Alkan | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.