Solved

Copying Files

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javap not working 8 60
Problem to Alipay 10 47
How to configure empty element in XML Document parser? 15 36
Eclipse Java import and method not resolved 4 52
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

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