Solved

How to copy file in Java?

Posted on 2001-08-26
7
356 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

21 Experts available now in Live!

Get 1:1 Help Now