程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Java-->多線程復制(文件指針),java--指針

Java-->多線程復制(文件指針),java--指針

編輯:JAVA綜合教程

Java-->多線程復制(文件指針),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 }

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved