Solved

Copying Files

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java DateChooser? 3 36
jboss wildfly 10.1 10 99
What's wrong with this code? 4 23
Java syntax, or is it Selenium 6 16
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

821 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