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