Link to home
Start Free TrialLog in
Avatar of Joegal
Joegal

asked on

Copy a file using Java

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.
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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();
               }
          }
     }
>> byte[] buffer = new byte[4096];
>> int count = in.read(buffer);

I'd prefer the buffered approach, but..
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);
               }
> I think objects should have removed that read line.

woops, yes I messed one. thanks rrz :)
The link in my comment is the answer to the question.
Thanks for accepting