?
Solved

Copying Files

Posted on 2002-03-14
3
Medium Priority
?
214 Views
Last Modified: 2010-03-31
People,

Which class, or set of classes facilitate the copying of files ?  Please note that I dont really want to open a file, read it in and write its contents to another file, i want to actually do a proper copy.  

There must be a class that will allow

myObj.copyFile("from", "to");
// or maybe from and to would be File objects.

Please tell me that there is a class that will do this.  A code snippet would be helpful too .

Cheers

Mehdi
0
Comment
Question by:mehdi
[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
3 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 6863996
As far as I know - there is no such functionality at least in the standard libraries. But why don't you want to create your own method for this? It will take only 5-6 lines of code
0
 
LVL 3

Accepted Solution

by:
chrisos earned 200 total points
ID: 6864268
Here you go...


package com.wintermuteis.util;

import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * <p>Title: File extension with copy facility</p>
 * <p>Description: File extension with copy facility</p>
 * <p>Copyright: Copyright (c) 2002 Wintermute Information Systems Ltd.</p>
 * @author Chris Cooper
 * @version 1.0
 */

public class FileWithCopy extends File {

    public FileWithCopy(File parent, String child) {
        super(parent, child);
    }

    public FileWithCopy(String pathName) {
        super(pathName);
    }

    public FileWithCopy(String parent, String child) {
        super(parent, child);
    }

    public void copy(String newFileName) throws IOException {
        File destination = new File(newFileName);
        copy(destination);
    }

    public void copy(File newFile) throws IOException {
        // Destination and streams
        FileInputStream fis = new FileInputStream(this);
        FileOutputStream fos = new FileOutputStream(newFile);

        // Amount of data to copy
        long fileLength = length();
        byte[] bytes = new byte[1024]; // 1K at a time
        int length = 0;
        long totalLength = 0;
        while (length > -1) {
            length = fis.read(bytes);
            if (length > 0) {
                fos.write(bytes, 0, length);
                totalLength += length;
            }
        }
        // Test that we copied all the data
        if (fileLength != totalLength) {
            throw new IOException("File copy size missmatch");
        }
        fos.flush();
        fos.close();
        fis.close();
    }

    // Test
    public static void main(String[] args) throws IOException {
        FileWithCopy copyme = new FileWithCopy("C:\\boot.ini");
        copyme.copy("C:\\boot_ini.COPY");
    }

}
0
 
LVL 1

Author Comment

by:mehdi
ID: 6864693
Cool, thanks
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

719 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