Solved

Copy a file using Java

Posted on 2004-04-15
9
204 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
session migration servlets 2 42
check java version using powershell 13 178
How to configure empty element in XML Document parser? 15 36
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

831 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