Solved

How to copy file in Java?

Posted on 2001-08-26
7
389 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
[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
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 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

734 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