473,574 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to copy an image file.

14 New Member
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.rea dLine())!=null)
{
fw.write(s);
fw.write(System .getProperty("l ine.separator") );
}
Aug 11 '08 #1
6 12495
JosAH
11,448 Recognized Expert MVP
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 Contributor
You may also try FileChannel class together with FileInputStream and FileOutputStrea m.
Aug 11 '08 #3
BigDaddyLH
1,216 Recognized Expert Top Contributor
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
moizpalitanawala
14 New Member
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"));
FileOutputStrea m fos=new FilePutputStrea m(new FIle("D:/copyImage.bmp") );
int c;
while((c=fis.re ad())!=-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 Recognized Expert Top Contributor
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
sagarsuman00
1 New Member
@moizpalitanawal a


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
2306
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: File name: ViewAttachment File type: From localhost 1) "ViewAttachment" is the name of the aspx page and not the image file. 2) Its not...
3
2802
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 seem to figure out the correct way to "unload" the file. I have a simple form with a picture box and a button. I am using dir() to get all the the...
1
4308
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 cURL code that would copy the $old_file to a $new_destination but did not have much success... basicaly i need to copy the file to the new server and...
3
1658
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 compared to creating an image file (.jpg) shall we say using photoshop?
1
1137
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. ...wondering if anyone can point me to an example or possibly get me rolling on this one? Thank you advance, I look forward to seeing a response to this...
2
2913
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 on the .NET Framework used (e.g., the Image class defines which formats can be used), or can different file formats be added after-the-fact...
7
2337
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 to prevent other websites from serving my image. For example - How can I prevent another website from doing this? <img...
1
2980
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 image upload in database: -----------Upload.jsp---------------- <html> <head> <title>Account Details </title>
1
2141
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 code is as follows; --------------------------------- // usings part using System.Drawing; using System.Drawing.Imaging;
3
4409
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 when iam using a normal web page... but can't in content page.... Code in Master Page <%@ Master Language="C#" AutoEventWireup="true"...
0
7732
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8243
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6456
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5302
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3742
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3754
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1062
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.