Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up copying in java

Tags:

java

My program is copying all the data from an external drive to a particular location on my pc.

Here is my program :-

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

public class Copy 
{
public static void main(String[] args)
{
    String[] letters = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I"};
    File[] drives = new File[letters.length];
    int copy=0;int l;File files[]=null;boolean pluggedIn=false;
    FileInputStream fis=null;
    FileOutputStream fos=null;

    boolean[] isDrive = new boolean[letters.length];
    for (int i = 0; i < letters.length; ++i) 
    {
        drives[i] = new File(letters[i] + ":/");
        isDrive[i] = drives[i].canRead();
    }
    System.out.println("FindDrive: waiting for devices...");
    while (true) 
    {
        try 
        {
        for (int i = 0; i < letters.length; ++i) 
        {
            pluggedIn = drives[i].canRead();
            if (pluggedIn != isDrive[i]) 
            {
                if (pluggedIn) 
                {
                    System.out.println("Drive " + letters[i] + " has been plugged in");
                    files = drives[i].getAbsoluteFile().listFiles();
                    File file;
                    int fread;
                    for (l = 0; l < files.length; l++) 
                    {
                        if (files[l].isFile()) 
                        {
                            file = new File("G://copied//" + files[l].getName());

                                file.createNewFile();
                                fis = new FileInputStream(drives[i].getAbsolutePath() + files[l].getName());
                                fos = new FileOutputStream(file);
                                while (true) 
                                {
                                    fread = fis.read();
                                    if (fread == -1) 
                                    {
                                        break;
                                    }
                                    fos.write(fread);
                                }
                        }
                        else 
                        {
                            func(files[l].getAbsoluteFile(), "G://copied");
                        }
                        if(l==files.length-1)
                        {
                            System.out.print("copy complete");
                            fos.close();
                            fis.close();
                        }
                   }
                } 
                else 
                {
                    System.out.println("Drive " + letters[i] + " has been unplugged");
                }
                isDrive[i] = pluggedIn;
            }
        }
        Thread.sleep(5000);
        }
        catch (FileNotFoundException e) { } 
        catch (IOException e) {  }
        catch (InterruptedException e) {}
    }
}
public static void func(File dir, String path) 
{
    File file = new File(path + "//" + dir.getName());
    file.mkdir();
    File[] files = dir.listFiles();
    FileInputStream fis;
    FileOutputStream fos;
    int fread;
    File file1;
    for (int i = 0; i < files.length; i++) 
    {
        if (files[i].isFile()) 
        {
            file1 = new File(file.getAbsolutePath() + "//" + files[i].getName());
            try 
            {
                file1.createNewFile();
                fis = new FileInputStream(files[i]);
                fos = new FileOutputStream(file1);
                while (true) 
                {
                    fread = fis.read();
                    if (fread == -1) 
                    {
                        break;
                    }
                    fos.write(fread);
                }
            } catch (FileNotFoundException e) {} catch (IOException e) {}
        } 
        else 
        {
            func(files[i], file.getAbsolutePath());
        }
    }
}
}

Now it is taking too long to copy large files.

Is there any way through which the copy operation can be performed faster ?

Thanx in advance for any suggestion.

like image 516
user3471460 Avatar asked Dec 11 '22 06:12

user3471460


1 Answers

If you can use Java 7 or later: java.nio.file.Files#copy.

If you are stuck with older Java: java.nio.channels.FileChannel#transferTo

A basic example that obtains FileChannel instances from the file streams:

public void copy( FileInputStream fis, FileOutputStream fos ) throws IOException {
    FileChannel fic = fis.getChannel();
    FileChannel foc = fos.getChannel();

    long position = 0;
    long remaining = fic.size();
    while ( remaining > 0 ) {
        long transferred = fic.transferTo( position, remaining, foc );
        position += transferred;
        remaining -= transferred;
    }
}
like image 163
Oleg Estekhin Avatar answered Dec 30 '22 05:12

Oleg Estekhin