Java Replication Folder

Source: Internet
Author: User

File.separator Slash uses this parameter instead to solve Java porting to its other platform compatibility issues


String Fsr=string.format ("D:%sceshi", file.separator);//folders that need to be copied do not include Ceshi
String Tsr=string.format ("D:%sdownloads", file.separator);//folder to be copied to
File F=new file (FSR);
File T=new file (TSR);
if (F.exists () &&f.isdirectory ())
{

File[] Filelist=f.listfiles ();
int fcout=filelist.length;
for (int i=0;i<fcout;i++)
{
Filechannelcopy (filelist[i],t);
}


}




Ways to copy Folders


public void Filechannelcopy (file s, file T) {


if (S.isdirectory ())
{
File Nf=new file (String.Format (T.getpath () +file.separator+s.getname ()));
if (!nf.exists ())
{
Nf.mkdir ();
}

File[] Filelist=s.listfiles ();
for (File fi:filelist)
{
Nf=new File (String.Format (Nf.getpath ()));
Filechannelcopy (FI,NF);
}
}
Else
{
FileInputStream fi = null;


FileOutputStream fo = null;


FileChannel in = null;


FileChannel out = null;


try {
T=new File (String.Format (T.getpath () +file.separator+s.getname ()));
fi = new FileInputStream (s);


fo = new FileOutputStream (t);


in = Fi.getchannel ();//get corresponding file channel


out = Fo.getchannel ();//get the corresponding file channel


In.transferto (0, In.size (), out);//Connect two channels and read from the in channel and write to the out channel


} catch (IOException e) {


E.printstacktrace ();


} finally {


try {


Fi.close ();


In.close ();


Fo.close ();


Out.close ();


} catch (IOException e) {


E.printstacktrace ();


}


}
}


}

Java Replication Folder

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.