Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Copying Files

Posted on 2002-03-14
3
Medium Priority
?
183 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

963 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