Solved

Copy a file using Java

Posted on 2004-04-15
9
208 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

695 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