在Java中,我们可以使用Runnable接口来设计线程,并实现文件下载的功能。下面是使用三种方式(节点流、处理流、工具类)下载文件的示例代码,并输出每种方式的时间。

  1. 使用节点流下载文件:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopyWithNodeStreams implements Runnable {
    private String sourceFile;
    private String destinationFile;

    public FileCopyWithNodeStreams(String sourceFile, String destinationFile) {
        this.sourceFile = sourceFile;
        this.destinationFile = destinationFile;
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();

        try (FileInputStream fis = new FileInputStream(sourceFile);
             FileOutputStream fos = new FileOutputStream(destinationFile)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        long endTime = System.currentTimeMillis();
        System.out.println("FileCopyWithNodeStreams Time: " + (endTime - startTime) + "ms");
    }
}
  1. 使用处理流下载文件:
import java.io.*;

public class FileCopyWithBufferedStreams implements Runnable {
    private String sourceFile;
    private String destinationFile;

    public FileCopyWithBufferedStreams(String sourceFile, String destinationFile) {
        this.sourceFile = sourceFile;
        this.destinationFile = destinationFile;
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();

        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destinationFile))) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        long endTime = System.currentTimeMillis();
        System.out.println("FileCopyWithBufferedStreams Time: " + (endTime - startTime) + "ms");
    }
}
  1. 使用工具类下载文件:
import org.apache.commons.io.FileUtils;

public class FileCopyWithUtils implements Runnable {
    private String sourceFile;
    private String destinationFile;

    public FileCopyWithUtils(String sourceFile, String destinationFile) {
        this.sourceFile = sourceFile;
        this.destinationFile = destinationFile;
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();

        try {
            FileUtils.copyFile(new File(sourceFile), new File(destinationFile));
        } catch (IOException e) {
            e.printStackTrace();
        }

        long endTime = System.currentTimeMillis();
        System.out.println("FileCopyWithUtils Time: " + (endTime - startTime) + "ms");
    }
}

在主程序中,可以创建多个线程来同时拷贝三个文件:

public class Main {
    public static void main(String[] args) {
        // 创建三个线程,分别拷贝三个文件
        Thread thread1 = new Thread(new FileCopyWithNodeStreams("sourceFile1.txt", "destinationFile1.txt"));
        Thread thread2 = new Thread(new FileCopyWithBufferedStreams("sourceFile2.txt", "destinationFile2.txt"));
        Thread thread3 = new Thread(new FileCopyWithUtils("sourceFile3.txt", "destinationFile3.txt"));

        // 启动线程
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

以上代码中,三个文件会同时被拷贝,每个线程负责一个文件的下载

java用Runable设计线程可以下载文件用三种方式:节点流处理流工具类需要输出每种的时间。三个文件拷贝要多线同时拷贝?

原文地址: https://www.cveoy.top/t/topic/iDrV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录