1.
public static long Forjava (File f1,file F2) throws exception{
long time=new Date (). Gettim E ();
int length=2097152;
fileinputstream in=new FileInputStream (F1);
fileoutputstream out=new fileoutputstream (F2);
byte[] buffer=new byte[length];
while (true) {
int ins=in.read (buffer);
if (ins==-1) {
in.close ();
out.flush ();
out.close ();
return new Date (). GetTime ()-time;
}else
out.write (buffer,0,ins);
}
}
2.
public static long Fortransfer (File f1,file F2) throws exception{
Long Time=new Date (). GetTime ();
int length=2097152;
FileInputStream in=new FileInputStream (F1);
FileOutputStream out=new FileOutputStream (F2);
FileChannel Inc=in.getchannel ();
FileChannel Outc=out.getchannel ();
int i=0;
while (true) {
if (Inc.position () ==inc.size ()) {
Inc.close ();
Outc.close ();
return new Date (). GetTime ()-time;
}
if ((Inc.size ()-inc.position ()) <20971520)
length= (int) (Inc.size ()-inc.position ());
Else
length=20971520;
Inc.transferto (Inc.position (), LENGTH,OUTC);
Inc.position (Inc.position () +length);
i++;
}
}
3.
public static long Forimage (File f1,file F2) throws exception{
Long Time=new Date (). GetTime ();
int length=2097152;
FileInputStream in=new FileInputStream (F1);
Randomaccessfile out=new randomaccessfile (F2, "RW");
FileChannel Inc=in.getchannel ();
Mappedbytebuffer Outc=null;
Mappedbytebuffer Inbuffer=null;
Byte[] B=new byte[length];
while (true) {
if (Inc.position () ==inc.size ()) {
Inc.close ();
Outc.force ();
Out.close ();
return new Date (). GetTime ()-time;
}
if ((Inc.size ()-inc.position ()) <length) {
length= (int) (Inc.size ()-inc.position ());
}else{
length=20971520;
}
B=new Byte[length];
Inbuffer=inc.map (Mapmode.read_only,inc.position (), length);
Inbuffer.load ();
Inbuffer.get (b);
Outc=out.getchannel (). Map (Mapmode.read_write,inc.position (), length);
Inc.position (B.length+inc.position ());
Outc.put (b);
Outc.force ();
}
}
4.
public static long Forchannel (File f1,file F2) throws exception{
Long Time=new Date (). GetTime ();
int length=2097152;
FileInputStream in=new FileInputStream (F1);
FileOutputStream out=new FileOutputStream (F2);
FileChannel Inc=in.getchannel ();
FileChannel Outc=out.getchannel ();
Bytebuffer B=null;
while (true) {
if (Inc.position () ==inc.size ()) {
Inc.close ();
Outc.close ();
return new Date (). GetTime ()-time;
}
if ((Inc.size ()-inc.position ()) <length) {
length= (int) (Inc.size ()-inc.position ());
}else
length=2097152;
B=bytebuffer.allocatedirect (length);
Inc.read (b);
B.flip ();
Outc.write (b);
Outc.force (FALSE);
}
}
5.
Import Java.io.FileInputStream; Import Java.io.FileOutputStream; Import Java.nio.channels.FileChannel;
public class Testtransfer {
public static void Main (string[] args) throws Exception {
FileInputStream fis = new FileInputStream ("C:\\abc.txt");
FileOutputStream fos = new FileOutputStream ("C:\\123.txt");
FileChannel FC1 = Fis.getchannel ();
FileChannel FC2 = Fos.getchannel ();
Fc2.transferfrom (FC1, 0, Fc1.size ());
Fc1.transferto (0,fc1.size (), FC2);
Fc1.close ();
Fc2.close ();
Fis.close ();
Fos.close ();
}
}
Java File replication