Solved

Copy a file using Java

Posted on 2004-04-15
9
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

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

I'd prefer the buffered approach, but..
0
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

726 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