Java-->多线程复制(文件指针)

--> 这里用到两种方法...其实也不算两种,就一点点不一样而已...

---> Test 测试类

package com.dragon.java.multithreadcopy;

import java.io.File;
import java.util.Scanner;

/*
 * 利用多线程复制文件1
 */
public class Test {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入文件路径:");
        File srcFile = new File(scanner.next());
        System.out.println("请输入线程数:");
        int n = scanner.nextInt();
        if (!srcFile.exists()) {
            System.out.println("该文件不存在!");
        }

        File desFile = new File(srcFile.getParent(), "new" + srcFile.getName());

     // 从线程数得到每个线程要复制的数据大小
long partLenghth = srcFile.length() / n + 1; for (int i = 1; i < n + 1; i++) {
       // 每次复制的开始和结束位置
new MyThread(srcFile, desFile, partLenghth * (i - 1), partLenghth * i).start(); } } }

--> MyThread类即线程实现类

package com.dragon.java.multithreadcopy;

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

public class MyThread extends Thread {
    private File srcFile;
    private File desFile;
    private long start;
    private long end;

    MyThread() {
        super();
    }

   // 构造方法传入源文件、目标文件、本次开始位置以及结束位置 MyThread(File srcFile, File desFile,
long start, long end) { super(); this.srcFile = srcFile; this.desFile = desFile; this.start = start; this.end = end; } @Override public void run() { RandomAccessFile rafSrc = null; RandomAccessFile rafDes = null; try { rafSrc = new RandomAccessFile(srcFile, "r"); rafDes = new RandomAccessFile(desFile, "rw");
       // 将文件指针移动到将要开始复制的位置 rafSrc.seek(start); rafDes.seek(start);
int len = -1; byte[] buffer = new byte[64]; while ((len = rafSrc.read(buffer)) != -1) { rafDes.write(buffer, 0, len);
          // 当读取到的位置大于或等于要结束的位置时跳出循环 if (rafSrc.getFilePointer() >= end) { break; } } } catch (IOException e) { System.out.println(e); } } }

--> -----------------------------------------------------------------------邪恶的分割线----------------------------------------------------------------------------

--> Test测试

import java.util.Scanner;

/*
 * 利用多线程复制文件利用多线程复制文件2
 */
public class Test {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入文件路径:");
        File srcFile = new File(scanner.next());
        System.out.println("请输入线程个数:");
        int n = scanner.nextInt();
        if (!srcFile.exists()) {
            System.out.println("该文件不存在!");
        }

        File desFile = new File(srcFile.getParent(), "new" + srcFile.getName());
long partLenghth = srcFile.length() / n + 1; for (int i = 1; i < n + 1; i++) {
       
// 每次传入单线程复制的长度 new MyThread(srcFile, desFile, partLenghth, MyThread.getPointer()) .start(); } } }

--> MyThread线程实现类

 1 package com.dragon.java.newthreadcopy;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.io.RandomAccessFile;
 6 
 7 public class MyThread extends Thread {
 8     private File srcFile;
 9     private File desFile;
10     private long partLength;
11     private static long pointer = 0;
12 
13     MyThread() {
14         super();
15     }
16 
17     MyThread(File srcFile, File desFile, long partLength, long pointer) {
18         super();
19         this.srcFile = srcFile;
20         this.desFile = desFile;
21         this.partLength = partLength;
22         MyThread.pointer = pointer;
23     }
24 
25     @Override
26     public void run() {
27         RandomAccessFile rafSrc = null;
28         RandomAccessFile rafDes = null;
29         try {
30             rafSrc = new RandomAccessFile(srcFile, "r");
31             rafDes = new RandomAccessFile(desFile, "rw");
32 
33             rafSrc.seek(pointer);
34             rafDes.seek(pointer);
35 
         // 一次性复制完整的一部分长度 36 byte[] buffer = new byte[(int) partLength]; 37 int len = rafSrc.read(buffer); 38 rafDes.write(buffer, 0, len); 39 pointer = rafSrc.getFilePointer(); 40 } catch (IOException e) { 41 System.out.println(e); 42 } 43 } 44 45 public static long getPointer() { 46 return pointer; 47 } 48 }

--> 感觉第二种也完全是多余的啊,就是一种方法...

posted on 2016-08-18 19:56  西门吃雪  阅读(1380)  评论(0编辑  收藏  举报

导航