Solved

Copying Files

Posted on 2002-03-14
3
206 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
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 50 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 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

9 Experts available now in Live!

Get 1:1 Help Now