473,396 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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;
while((s=br.readLine())!=null)
{
fw.write(s);
fw.write(System.getProperty("line.separator"));
}
Aug 11 '08 #1
6 12479
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

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

Similar topics

4
by: moondaddy | last post by:
Using vb.net I need to download image files to the client browser where they can save to disk. Below is some sample code I'm using. when I run this the File Download window in the browser says: ...
3
by: Scott | last post by:
Hello, I am running into a problem with my code and can't seem to figure out the solution. I know it has to do with the pciture box control and unloading the image inthe picture box but I can't...
1
by: friglob | last post by:
i would need help with creating some cURL code... $old_file = "http://my_host.com/images/test.jpg"; $new_destination = "http://some_other_host.com/archived_images"; i was trying to create a...
3
by: shotokan99 | last post by:
hi, shall we say you want to creae a banner, a button, and other images for your site. some create images on the fly through php using gd library. is this more advisable or more efficient...
1
by: z9redila | last post by:
Hello - I would like to build a script that would read a number of file names from a directory (number of files is unknown at run time) and then save them to a .txt file name on my pc. ...
2
by: Peter Oliphant | last post by:
The Image class allows loading a bitmap from a graphic file. So far I've gotten it to work with JPG and BMP files. What other graphic file formats are supported in this way? Is this fixed based...
7
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi. I have an ASP.NET 2.0 web application which contains an Images directory with all website images. How can I prevent other websites from creating img tags with the source as my images? I want...
1
by: amritranjan | last post by:
How to retrive image file from MS access database and display this in another JSPpage -------------------------------------------------------------------------------- This is my Jsp code for...
1
by: Sinan Alkan | last post by:
Hi All, I have a method to resize any image file to the specific dimensions and save this new image to a path. I found it on a web page(By Joel Neubeck) and i changed it for my project. The...
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.