Link to home
Start Free TrialLog in
Avatar of mehdi
mehdi

asked on

Copying Files

People,

Which class, or set of classes facilitate the copying of files ?  Please note that I dont really want to open a file, read it in and write its contents to another file, i want to actually do a proper copy.  

There must be a class that will allow

myObj.copyFile("from", "to");
// or maybe from and to would be File objects.

Please tell me that there is a class that will do this.  A code snippet would be helpful too .

Cheers

Mehdi
Avatar of 108093233315
108093233315

In Java there is no way to copy a file without opening it. Other solutions are platform dependend and not possible in Java.
Here is a simple Example of the copyFile() method:

public void copyFile(String src, String dest) throws IOException
{
  InputStream is = new FileInputStream(src);
  OutputStream os = new FileOutputStream(dest);
  byte[] buffer = new byte[0xffff]; // buffer size = FFFF hex
  int nbytes;

  while ((nbytes = is.read(buffer)) != -1)
  {
    os.write(buffer, 0, nbytes);
  } // while ((nbytes = is.read(buffer)) != -1)

  is.close();
  os.close();
} // public void copyFile(String)
Avatar of mehdi

ASKER

This question has already been answered.. i posted the question twice by accident.. darn refresh :)

Sorry.. I will now write and ask them to remove this repeat question.

Mehdi
ASKER CERTIFIED SOLUTION
Avatar of Moondancer
Moondancer

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