Solved

How to copy file in Java?

Posted on 2001-08-26
7
373 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
fibonacci ten numbers 4 39
servlet filter example 37 63
Chrome and Firefox Java 5 33
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

777 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