IO streaming-file copy, delete

Source: Internet
Author: User
Tags file copy

Io Flow method

1 Copy files, folders

2 Delete files, folders

Second, code implementation

1 copy files, folders//Copy all files from any known directory to another directory
public static void Copyall (file srcfile, file destfile) throws Exception {
if (srcfile.exists ()) {
if (!destfile.exists ()) {
Destfile.mkdirs ();
}
file[] File = Srcfile.listfiles ();
InputStream is = null;
OutputStream OS = null;
for (File f:file) {
if (F.isfile ()) {
is = new FileInputStream (f);
OS = new FileOutputStream (new File (DestFile, F.getname ()));
int len = 0;
byte[] byt = new byte[1024];
while ( -1!= (len = Is.read (byt))) {
Os.write (byt, 0, Len);
}
try {
Os.close ();
Is.close ();
catch (Exception e) {
E.printstacktrace ();
}
} else {
Copyall (F, New File (DestFile, F.getname ()));
}
}


} else {
System.out.println ("Source file does not exist");
}
}

2 Delete files, folders

Delete folders and files under the specified file

public static void Delfile (file file) {

if (file.exists ()) {

File[] Flist=file.listfiles ();

for (File f:flist) {

if (F.isfile ()) {

F.delete ();

}else{

Delfile (f);

}

}

Delete the empty folder when all the files in the folder are deleted

File.delete ();

}else{

SYSTEM.OUT.PRINTLN ("Original document does not exist");

}

}

Third, attention

1 code can be used, but pay attention to the call in Main.




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.