469,275 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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;
while((s=br.readLine())!=null)
{
fw.write(s);
fw.write(System.getProperty("line.separator"));
}
Aug 11 '08 #1
6 11489
JosAH
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,

Jos
Aug 11 '08 #2
sukatoa
539 512MB
You may also try FileChannel class together with FileInputStream and FileOutputStream.
Aug 11 '08 #3
BigDaddyLH
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;
while((c=fis.read())!=-1)
{
fos.write(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.

Moiz
Aug 12 '08 #5
BigDaddyLH
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
@moizpalitanawala


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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.