在最短的时间内将外部驱动器的所有文件复制到pc

时间:2014-04-02 10:02:54

标签: java

我正在尝试将外部驱动器的所有文件复制到我电脑上的特定位置。

这是我的代码: -

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

public class mod
{
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 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...");
    File file;
    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();
                    for (l = 0; l < files.length; l++) 
                    {
                        if (files[l].isFile()) 
                        {
                            file = new File("G://copied//" + files[l].getName());
                            fis = new FileInputStream(drives[i].getAbsolutePath() + files[l].getName());
                            fos = new FileOutputStream(file);
                            byte[] buffer = new byte[99999999];
                            int n;
                            while ((n = fis.read(buffer)) != -1)
                            {
                                fos.write(buffer, 0, n);
                            }
                            fis.close();
                            fos.close();        
                        }
                        else 
                        {
                            func(files[l].getAbsoluteFile(), "G://copied");
                        }
                        if(l==files.length-1)
                        {
                            System.out.print("copy complete");
                        }
                   }
                } 
                else 
                {
                    System.out.println("Drive " + letters[i] + " has been unplugged");
                }
                isDrive[i] = pluggedIn;
            }
        }
        Thread.sleep(3000);
        }
        catch (FileNotFoundException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); }
        catch (InterruptedException e) { e.printStackTrace(); }

    }
}
public static void func(File dir, String path) throws IOException 
{
    File file = new File(path + "//" + dir.getName());
    file.mkdir();
    File[] files = dir.listFiles();
    FileInputStream fis=null;
    FileOutputStream fos=null;
    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) 
                {
                    byte[] buffer = new byte[99999999];
                    int n;
                    while ((n = fis.read(buffer)) != -1)
                    {
                        fos.write(buffer, 0, n);
                    }
                    fis.close();
                    fos.close();
                }
            } catch (FileNotFoundException e) {} catch (IOException e) {}
        } 
        else 
        {
            func(files[i], file.getAbsolutePath());
        }
    }
   }
}

工作正常。但是复制大文件需要很长时间。

我的问题是,有没有办法可以减少复制时间?

0 个答案:

没有答案