Solved

Copying Files

Posted on 2002-03-14
3
176 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:mehdi
3 Comments
 

Expert Comment

by:108093233315
Comment Utility
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)
0
 
LVL 1

Author Comment

by:mehdi
Comment Utility
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
0
 
LVL 1

Accepted Solution

by:
Moondancer earned 0 total points
Comment Utility
Thank you, 50 points have been refunded to you for this duplicate question.

As you are aware, this can be avoided by not hitting submit more than once, not using your Browser's back, refresh/F5 functions, but rather our application interface's Reload button when needed.

Item closed and moved to the PAQ at zero points rather than deleting at this time so you are advised of this refund.

Moondancer - EE Moderator
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 step-by-step guide to install VisualVM launcher in eclipse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now