Solved

Copying Files

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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…
Suggested Courses

630 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