Solved

How to copy file in Java?

Posted on 2001-08-26
7
395 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

630 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