Java multithreaded Replication file (RPM) __java

Source: Internet
Author: User
Tags file copy
Package MASTERCN;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.InputStream;
Import Java.io.RandomAccessFile;


public class CopyFile implements Runnable {
source file
Private String sourceFileName;
Destination file
Private String TargetFileName;
Total Blocks
private int blockcount;
Start copy of block serial number
private int blockno;
Cache size
private int maxbuffsize = 1024 * 1024;
/**
* Copy the sourceFileName file into the Blockno block after blockcount block to sourceFileName
* @param sourcefilename Source file name
* @param targetfilename Target file name
* @param blockcount File Block copy number
* @param blockno start the copy block serial number
*/
Public CopyFile (String sourcefilename,string targetfilename, int blockcount, int blockno)
{
this. sourceFileName = sourceFileName;
this. TargetFileName = TargetFileName;
this. Blockcount = Blockcount;
this. Blockno = Blockno;
}
public void Run () {
Get source file
File File = new file (sourceFileName);
Get the size of the source file
Long size = File.length ();
Calculates the size of a single block based on the size of the file and the total number of blocks
Long blocklenth = Size/blockcount;
Figure out where copy is currently started
Long startposition = Blocklenth * BLOCKNO;
Instantiating a cache
byte [] buff = new byte [maxbuffsize];
try {
Get input stream from source file
InputStream InputStream = new FileInputStream (sourcefilename);
Get the random Access object of the target file
Randomaccessfile RAF = new Randomaccessfile (TargetFileName, "RW");
Offset the target file's pointer to the start position
Raf.seek (startposition);
The number of bytes currently being read
int curredlength;
Cumulative number of bytes read and
int totalredlength = 0;
Offset the source file's pointer to the start position
Inputstream.skip (startposition);
Read files in blocks
while ((curredlength = Inputstream.read (buff)) > 0 && totalredlength < blocklenth)
{
Write bytes in cache to file? In the destination file
Raf.write (Buff, 0, curredlength);
Cumulative number of bytes read
Totalredlength + = Curredlength;
}
Close related Resources
Raf.close ();
Inputstream.close ();
catch (Exception ex)
{
Ex.printstacktrace ();
}
}

}
Test.java
Package MASTERCN;

public class Test {

/**
* @param args
*/
source file
private static String sourcefile;
Destination file
private static String targetfile;
Divided number of blocks
private static int blockcount;

public static void Main (string[] args) {
TODO auto-generated Method Stub
//
Sourcefile=args[0];
TARGETFILE=ARGS[1];
Blockcount=integer.parseint (args[2]);
Record start time
Long Begintime=system.currenttimemillis ();
Block file copy in turn
for (int i=0;i<blockcount;i++)
{
Instantiating a file Copy object
CopyFile copyfile=new CopyFile (sourcefile,targetfile,blockcount,i);
Instantiating threads
Thread thread=new thread (copyfile);
Start thread
Thread.Start ();
Try
{
Join thread
Thread.Join ();
}
catch (Exception e) {
Todo:handle exception
E.printstacktrace ();
}
}
Calculation time consuming
Long Endtime=system.currenttimemillis ();
Output time consuming
System.out.println ("Common:" + (Endtime-begintime) + "MS");

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.