Solved

Copy a file using Java

Posted on 2004-04-15
9
197 Views
Last Modified: 2008-03-10
Is there a better way of copying a file using Java than the following one?


public static void copyFile(File src, File dest) throws IOException {
            FileInputStream in = null;
            FileOutputStream out = null;
            try {                  
                  in = new FileInputStream(src);
                  out = new FileOutputStream(dest);

                  ByteArrayOutputStream outBuf =
                        new ByteArrayOutputStream((int) src.length());

                  byte[] buffer = new byte[4096];
                  int count = in.read(buffer);
                  while (count > 0) {
                        outBuf.write(buffer, 0, count);
                        count = in.read(buffer);
                  }
                  out.write(outBuf.toByteArray());
            } finally {
                  try {
                        if (in != null)
                              in.close();
                  } catch (IOException ex) {
                        ex.printStackTrace();
                  }
                  try {
                        if (out != null)
                              out.close();
                  } catch (IOException ex) {
                        ex.printStackTrace();
                  }
            }
      }

Thanks.
0
Comment
Question by:Joegal
9 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 10835998
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10836140
Well - strictly speaking what's happening there is you're writing a file into a byte array. Is that what you want to do? If so, that's fine
0
 
LVL 92

Expert Comment

by:objects
ID: 10837783
public static void copyFile(File src, File dest) throws IOException {
          FileInputStream in = null;
          FileOutputStream out = null;
          try {              
               in = new FileInputStream(src);
               out = new FileOutputStream(dest);

               byte[] buffer = new byte[4096];
               int count = in.read(buffer);
               while ((count=in.read(buffer))!=-1) {
                    out.write(buffer, 0, count);
               }
          } finally {
               try {
                    if (in != null)
                         in.close();
               } catch (IOException ex) {
                    ex.printStackTrace();
               }
               try {
                    if (out != null)
                         out.close();
               } catch (IOException ex) {
                    ex.printStackTrace();
               }
          }
     }
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10839440
>> byte[] buffer = new byte[4096];
>> int count = in.read(buffer);

I'd prefer the buffered approach, but..
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Expert Comment

by:rrz
ID: 10842011
I think objects should have removed that read line.
>          try {              
               in = new FileInputStream(src);
               out = new FileOutputStream(dest);

               byte[] buffer = new byte[4096];
           ///////    int count = in.read(buffer);
               while ((count=in.read(buffer))!=-1) {
                    out.write(buffer, 0, count);
               }
0
 
LVL 92

Expert Comment

by:objects
ID: 10846411
> I think objects should have removed that read line.

woops, yes I messed one. thanks rrz :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11047416
The link in my comment is the answer to the question.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11140944
Thanks for accepting
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now