Solved

Copying Files

Posted on 2002-03-14
3
181 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
[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
3 Comments
 

Expert Comment

by:108093233315
ID: 6864683
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
ID: 6864699
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
ID: 6879768
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

631 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