Speed ​​up copying in java

My program copies all data from an external drive to a specific location on my computer.

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 copying large files takes too much time.

Is there a way that a copy operation can be faster?

Thanx in advance for any suggestion.

+4
source share
3 answers

You must use a buffer. Copy logic should look something like this:

byte[] buffer = new byte[4096];
int n;
while ((n = input.read(buffer) != -1)
{
    output.write(buffer, 0, n);
}
output.close();
input.close();

Thus, you copy a fragment of 4096 bytes at a time, instead of byte per byte.

+4
source

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

Java: java.nio.channels.FileChannel # transferTo

, FileChannel :

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;
    }
}
+8
                            file.createNewFile();

Remove it. This is redundant. new FileOutputStream()will do it anyway. You simply add processing here and process the disk.

                            fis = new FileInputStream(drives[i].getAbsolutePath() + files[l].getName());
                            fos = new FileOutputStream(file);

Now add:

int count;
byte[] buffer = new byte[8192]; // or much more if you can afford the space
while ((count = fis.read(buffer)) > 0)
{
    fos.write(buffer, 0, count);
}

Return to your code:

                            while (true) 
                            {
                                fread = fis.read();
                                if (fread == -1) 
                                {
                                    break;
                                }
                                fos.write(fread);
                            }

Remove it all. Reading a byte at a time is just as inefficient as it gets.

+1
source

Source: https://habr.com/ru/post/1533861/


All Articles