使用RandomAccessFile和多线程来实现开启三个线程去复制一个文件

题目:使用RandomAccessFile和多线程来实现开启三个线程去复制一个文件
代码:
package Down;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class DownLoadDemo {
public static void main(String[] args) {
File src = new File(“E:\qqq.txt”);
File dst = new File(“E:\ppp.txt”);
DownloadUtil downloadUtil = new DownloadUtil(src, dst, 3);
downloadUtil.download();
}
}

// 下载工具类
class DownloadUtil {
private File src;// 源文件
private File dst;// 目标文件
private int threadNum;// 线程数量

public DownloadUtil(File src, File dst, int threadNum) {
    super();
    this.src = src;
    this.dst = dst;
    this.threadNum = threadNum;
}

// 开启多线程下载
public void download() {
    // 循环开多个线程
    for (int i = 0; i < threadNum; i++) {
        DownloadThread thread = new DownloadThread(i);
        thread.setName("线程" + (i + 1));
        thread.start();
    }
}

// 定义一个下载线程
class DownloadThread extends Thread {
    // private int index;/表示第几个线程
    // 每一个线程要下载的文件大小
    // 如果不能平方,则每一份+1,确保下载完整个文件
    private long BlockSize = src.length() % threadNum == 0 ? src.length() / threadNum
            : src.length() / threadNum + 1;
    // 起始位置
    private long startPos;

    public DownloadThread(int index) {
        // this。index = index;
        // 计算初始位置
        startPos = BlockSize * index;
    }

    public void run() {
        super.run();
        try {
            // 要读入的文件
            RandomAccessFile rafR = new RandomAccessFile(src, "r");
            // 要写入的文件
            RandomAccessFile rafW = new RandomAccessFile(dst, "rw");
            rafR.seek(startPos);
            rafW.seek(startPos);
            int bufSize = 1024 * 1024;// 缓冲区大小
            byte[] buf = new byte[bufSize];// 缓冲区
            int len = -1;
            do {
                len = (int) (BlockSize > bufSize ? bufSize : BlockSize);
                rafR.read(buf, 0, len);
                rafW.write(buf, 0, len);
                BlockSize -= len;
                System.out.println(Thread.currentThread().getName() + "读取了" + len + "字节");
            } while (BlockSize > 0);
            rafR.close();
            rafW.close();
            System.out.println(Thread.currentThread().getName() + "下载完毕");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RandomAccessFileJava 中一种可以随机访问文件的类,可以对文件进行读写操作。多线程读取文件可以提高读取的效率,以下是一个简单的多线程读取文件的示例代码: ```java import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; public class MultiThreadReadFile { private static final int THREAD_COUNT = 4; // 线程数 private static final int BUFFER_SIZE = 1024; // 缓冲区大小 private static final String FILE_PATH = "test.txt"; // 文件路径 public static void main(String[] args) { File file = new File(FILE_PATH); long fileLength = file.length(); // 获取文件长度 long blockSize = fileLength / THREAD_COUNT; // 计算每个线程读取的块大小 for (int i = 0; i < THREAD_COUNT; i++) { long start = i * blockSize; // 计算每个线程读取的起始位置 long end = (i == THREAD_COUNT - 1) ? fileLength : (i + 1) * blockSize; // 计算每个线程读取的结束位置 new Thread(new ReadFileThread(start, end)).start(); // 创建并启动线程 } } static class ReadFileThread implements Runnable { private long start; private long end; public ReadFileThread(long start, long end) { this.start = start; this.end = end; } @Override public void run() { try { RandomAccessFile raf = new RandomAccessFile(FILE_PATH, "r"); raf.seek(start); // 定位到起始位置 byte[] buffer = new byte[BUFFER_SIZE]; int length; while ((length = raf.read(buffer)) != -1 && raf.getFilePointer() <= end) { // 读取文件 System.out.print(new String(buffer, 0, length)); } raf.close(); // 关闭文件 } catch (IOException e) { e.printStackTrace(); } } } } ``` 该示例代码中,多个线程通过计算每个线程需要读取的文件块的起始位置和结束位置,分别读取文件。其中,每个线程使用RandomAccessFile 类来读取文件,并且使用一个缓冲区来提高读取效率。在读取结束后,需要关闭文件流。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值