Solved

How to copy file in Java?

Posted on 2001-08-26
7
379 Views
Last Modified: 2012-08-13
Hi,

I want to know how to copy file in Java? In VC++, it has a function CopyFile to do that but i can't find it in Java. It has other method to do so in Java?

thanks

Gene
0
Comment
Question by:gene777
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6427529
Unfortunatelt there is no copy method in Java.
You have to do it by hand ie. open and read file, and write to copy file.

Alternatively you can use a platform dependant solution using System.exec() to call the OS's copy command.
0
 
LVL 1

Expert Comment

by:yaseransari
ID: 6427776
Hello gene777
Though copy is not possible through a method as such,
you can "cut paste" a file  with the method renameTo() of the class File.
However if you just want to copy you will have to do it manually as objects mentioned.

Regards

Yaser

0
 

Expert Comment

by:bgkyer
ID: 6430779
You can do something like this:

      // read in file
      File                 myFileIn               = null;
      FileInputStream      myFileInputStream      = null;
      BufferedInputStream  myBufferedInputStream  = null;

      myFileIn               = new File (aSrcFileName);
      myFileInputStream      = new FileInputStream   (myFileIn);
      myBufferedInputStream  = new BufferedInputStream (myFileInputStream);

      // write file to new name (a copy like operation)
      File                 myFileOut              = null;
      FileOutputStream     myFileOutputStream     = null;
      BufferedOutputStream myBufferedOutputStream = null;

      myFileOut              = new File (aDestFileName);
      myFileOutputStream     = new FileOutputStream  (myFileOut);
      myBufferedOutputStream = new BufferedOutputStream (myFileOutputStream);

      for (long i = 0L; i < myFileIn.length (); i++)
      {
         myBufferedOutputStream.write (myBufferedInputStream.read ());
      }

      myBufferedOutputStream.flush ();
      myBufferedOutputStream.close ();
      myBufferedInputStream.close ();


 Where you supply aSrcFileName and aDestFileName.

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

Expert Comment

by:JokerBoy
ID: 6446109
As far as i know there is no copy file method in Java. Instead you can use the following code:

BufferedInputStream file1 = new BufferedInputStream(new FileInputStream("c:/dir/new/file1.txt"));

BufferedOutputStream file2 = new BufferedOutputStream(new FileOutputStream("c:/dir/new/file45.txt"));

     while( file1.available() > 0)
          file2.write(file1.read());
     file1.close();
     file2.close();
0
 
LVL 27

Expert Comment

by:rrz
ID: 6446136
import java.io.*;
public class Copy {
    public static void main(String[] args) throws IOException {
     File firstFile = new File("file1.txt");
     File secondFile = new File("file2.txt");

        FileReader in = new FileReader(firstFile);
        FileWriter out = new FileWriter(secondFile);
        int c;

        while ((c = in.read()) != -1)
           out.write(c);

        in.close();
        out.close();
    }
}
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6727946
This question is LOCKED with a Proposed Answer.  If it helps you, great, accept it and grade it to close.  If not, reject it and comment as to why or what else is needed.

Few additional experts will join this collaboration effort, once a question has been locked.  Just want to confirm this to you.  If more than one expert helps you, you can always split points or award additional help in a new question, within the same topic area.  If you need help from us, post a zero point question here, and include the link:

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Moondancer
Community Support Moderator @ Experts Exchange
0
 

Expert Comment

by:SpideyMod
ID: 8289702
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
how to install java on RHEL image on EC2 4 41
Java Eclipse Loop 3 21
What browser will run Java? 7 69
What is the use of Forwarding Class in java 1 18
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

820 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