Java --> multi-thread replication (File pointer), java -- pointer
--> Two methods are used here... actually, they are not two. They are just a little different...
---> Test class
Package com. dragon. java. multithreadcopy; import java. io. file; import java. util. optional;/** use multiple threads to copy the file 1 */public class Test {public static void main (String [] args) {@ SuppressWarnings ("resource ") required bytes = new bytes (System. in); System. out. println ("Enter the File path:"); File srcFile = new File (paths. next (); System. out. println ("Enter the number of threads:"); int n = threads. nextInt (); if (! SrcFile. exists () {System. out. println ("this file does not exist! ");} File desFile = new File (srcFile. getParent ()," new "+ srcFile. getName ());
// Obtain the data size to be copied by each thread from the number of threads. long partLenghth = srcFile. length ()/n + 1; for (int I = 1; I <n + 1; I ++ ){
// Start and end of each copy. new MyThread (srcFile, desFile, partLenghth * (I-1), partLenghth * I). start ();}}}
--> The MyThread class is the thread implementation class.
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 ();}
// Constructor: input the source File, target File, start position, and end position 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 ");
// Move the file pointer to the location where you want to start copying 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 );}}}
--> ------------------------------------------------------------------------- The evil split line ----------------------------------------------------------------------------
--> Test
Import java. util. optional;/** use multiple threads to copy files 2 */public class Test {public static void main (String [] args) {@ SuppressWarnings ("resource ") required bytes = new bytes (System. in); System. out. println ("Enter the File path:"); File srcFile = new File (paths. next (); System. out. println ("Enter the number of threads:"); int n = threads. nextInt (); if (! SrcFile. exists () {System. out. println ("this file does not exist! ");} File desFile = new File (srcFile. getParent ()," new "+ srcFile. getName ());
Long partLenghth = srcFile. length ()/n + 1; for (int I = 1; I <n + 1; I ++ ){
// The length of each input single-thread ReplicationNew MyThread (srcFile, desFile, partLenghth, MyThread. getPointer (). start ();}}}
--> MyThread thread implementation class
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 @ Override26 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
// One-time replication of a complete Part Of The length 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}
--> I think the second method is completely redundant. It is just a method...