java - 加快在java中的复制

标签 java

我的程序正在将所有数据从外部驱动器复制到我电脑上的特定位置。

这是我的程序:-

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());
        }
    }
}
}

现在复制大文件需要很长时间。

有什么方法可以更快地执行复制操作?

提前感谢任何建议。

最佳答案

如果您可以使用 Java 7 或更高版本: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;
    }
}

关于java - 加快在java中的复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22706136/

相关文章:

java - Android Studio 在 DefaultHttpClient 中获取错误

java - 将 Eclipse 项目工作区导入 Apache tomcat 文件夹

java - Mockito 中的模拟类正在调用什么构造函数(如果有)?

java - 通过 ModelMapper 映射时将 LocalDateTime.now() 的值设置为两个字段

java - Java程序监听数据库事件

java - 如果初始化对象涉及检查异常处理,如何使用枚举创建单例

java - 使用 Spring Boot 应用程序作为 gradle 的依赖项

java - 从 MATLAB 执行时如何为 JAVA 程序分配更多内存?

java - JSP/Servlet 验证 - 转发与重定向

java - 在 Java/处理中清空数组