Likes

Java Lab_14_Ans_2

2. Write Java classes that use the FileVisitor class to recursively copy one directory to another.



SOLUTION


1. Open the project RecursiveCopyExercise in the directory D:\labs\ll-NI0.2\practices.
2. Expand the Source Package* folder and subfolders and look at the Copy, java class.
a. Note that the Copy, java class contains the mair method.
b. The application takes two arguments, a source and target paths.
c. If the target file or directory exists, the user is prompted whether to overwrite.
d. If the answer is yes (or the letter y). the method continues.
e. An instance of the CopyFileTree class is created with the source and target.
f. This mstance is then passed to the walkFileTree method (with the source Path object).
You will need to prcrnde method bodies for the methods in the CopyFileTree. java class.
3. Open the CopyFileTree. java class.
a. This class unplements the 'ileVisitcr interface. Note that FileVisitcr is a generic interface, and this interface is boxed with the Rath class. This allows the interface to denne the t>pe of the arguments passed to its methods.
b. The CopyFileTree implements all the methods denned by FiieVisitor.
c. Your task is to write method bodies for the oreVisitDirectory and visitFile methods. You will not need the po;tVi; it Directory method. and you have been provided a method body for the visitrileFailed metliod.
4. Write tlie metliod body for preVicitDirectory. This metliod is called for the starting node of tlie tree and every subdirectory. Therefore, you should copy tlie directory of tlie source to tlie target. If tlie file already exists, you can ignore that exception (because you are domg tlie cops- because tlie user elected to overwrite.)
a. Start by creatmg a new directors- that is relative to tlie target passed in. but is tlie node name from tlie source. The metliod call to do this is:
Path newdir = target.resolve(source.relativi:e(dir));
b. In a try block cops- tlie directors- passed to tlie oreVicitDirectory metliod to tlie newdir that you created.
c. You can ignore any File Already Exacts Except ion thrown, because you are overwrituig any existmg folders and files in tins cops-.
d. Catch air.- other IOExcepticnc. and use the 51CIP_SUBTPEE return to avoid repeated errors.
5. Write tlie metliod bods- for tlie /isitFiie metliod. This metliod is called when tlie node reached is a file. The file is passed as an argument to tlie metliod.
a. As with tlie preVisitOirectory. you must rationalize tlie file reached (source path) with tlie path that you wanted for tlie target. Use tlie same metliod call as above (only usmg -ile instead of dir):
b. As m the sreVisitDirectory method use the 'iles.copy method in a try block. Mala sure that you pass 3EPLACE_EXISTING m as an option to overwrite air.- existing file in the directory.
c. Catch air.- lOExceptico thrown and report an error.
d. Fix air.- missing unports.
e. Save your class.
6. Test your application by copymg a directory (ideally with subdirectories) to another location on the disk. For example, cops- the D: \labs\ll-NI0.2 directory to D: \Temp.
a. Right-click the project and select Properties.
b. Click Run.
c. Enter the following as Arguments:
D:\labs\ll-NI0.2 D:\Terp
d Click OK.
7. Run the project and you should see the follow mg message:
Successfully copied D:\labs\ll-NI0.2 to D:\Tenp
a. Run the project agaui and you should be prompted:
Target directory exists, ^.-erv.rite existing files? (yes/oo):

No comments: