Java-io Flow Exercises

Source: Internet
Author: User

1. Receive two folder paths from the keyboard, copy one of the folders (containing content) to another folder
Import Java.io.file;import java.io.fileinputstream;import java.io.fileoutputstream;import java.io.IOException;        Import Java.util.scanner;public class Dircopydemo {public static void main (string[] args) throws IOException {        Scanner s = new Scanner (system.in);        SYSTEM.OUT.PRINTLN ("Input source directory path");        String S1 = S.nextline ();        SYSTEM.OUT.PRINTLN ("Input destination directory path");        String s2 = s.nextline ();        File from = new file (S1);        File to = new file (s2);        if (!to.exists ()) {to.mkdirs ();    } copydir (from, to);        } private static void Copydir (file from, file to) throws IOException {FileInputStream FIS = null;        FileOutputStream fos = null;        file[] froms = From.listfiles ();        int len=0;        byte[] WJ = new byte[1024]; for (File f:froms) {if (F.isdirectory ()) {///If it is a folder, you need to create a folder with the same name in to,//and then Recursively to the folder, copy the corresponding file//for the created folder name, you need to get to the absolute path to the destination, and then add the name of F               File NewFile = new file (To.getabsolutepath () + "\ \" + f.getname ());                Newfile.mkdirs ();            Here you need to go inside the new folder for recursion//to replicate files within the folder Copydir (f, newfile);  } else {//the file name in the output destination to must first get the file name of F and then add the absolute path of file2//so that you can create a file with the same name FIS                = new FileInputStream (f);                fos= New FileOutputStream (To.getabsolutepath () + "\ \" +f.getname ());                    while (len = Fis.read (WJ))! =-1) {fos.write (WJ, 0, Len);                Fos.flush ();                  } fis.close ();              Fos.close (); }        }    }}

Java-io Stream Exercise

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.