All right. So in this video, let's review deleting and copying operations
using files methods, which operate on path instances.
All right. So we need a new class in our IOProject
FilesCopyMoveDelete will be what I'll call it
FilesCopyMoveDelete.
So this instance, we've got a method named testCreationFromPathInstance,
which will try to create a file using a relative path.
Now this directory structure does not yet exist in my current working directory
and you can see that we're invoking the method from our main method.
Execute this code,
you can see that it's given us a no such file exception
and that's because the Files.createFile method
will not create directories.
So if the directories do not exist this operational file and you saw that that did file here.
Let's test out the create directory
method next.
I'm going to append this code in the same method
a testCreationFromPathInstance
and the comment does give it away, but let's run this now.
And you can see we've got the same error there "Unable to create directory,
NoSuchFileException
and then the directory we're trying to correct testA\testB.
All right. Let's append some more code in the same method at the bottom.
And here you can see what I'm doing is first
using createDirectories on the path instance at the same path
we've used previously.
Then using the isDirectory method of the instance returned to see what was created
and then deleting the path instance using Files.Delete
which deletes the leaf element preferred referenced on the path.
Let's run this.
You can see create a directory
a big long directory again because I'm back on my network drive.
And we got the output also "Path created is a directory".
And looking at the project pane,
we've now got a testA folder, we've got a testB folder and a testC folder,
but we don't have a file named testFile.txt
in that testC subdirectory.
Let's actually add some code to create that
and again at the end of this method.
We'll go briefly to full screen.
But then we'll open the project pane while we run this
so we can hopefully see that file being created. Run this.
You saw the file in the project pane get created.
You might have noticed that the last code was redundant
since the code I started the example with
was successful once all the directories existed.
You can see there the output option A
it actually created the file there because the directory there existed.
Still though it's interesting to see the exception,
FileAlreadyExistsException which was thrown and you can see the output for that
at the bottom of the output pane.
Let's add some cleanup work so that this code executes the same each time.
I'm going to add a new method here.
And this will do some deletions. In this case, I just want to delete the file if it exists.
This does not delete the directories created.
So I'm going to go back up to my main method.
I'm going to add this before
we actually call the testCreationFromPathInstance method.
Let’s run this.
We can see from the output, the code successfully deleted the file,
but not the directories that were created
and obviously, the file was then recreated as well.
All right. So going back to our doDelete method
or doDeletions method I should say.
I'm going to append some code to the bottom of that.
I'm adding this to demonstrate what happens if you attempt to delete a file that does not exist.
You can see we've got a NoSuchFileException
that's because the file was already deleted in the previous step.
Though using delete throws an exception that the file does not exist,
but using Files.delete if exists does not.
All right. Let's just add one last bit of code to our doDeletions method.
So this code will recursively delete directories
using a path instance representing a directory.
When the path is null, it's in the current directory.
A directory is only deleted when its contents are empty.
I'll be using an alternative example to show you deleting directories using the walk method
in an upcoming video.
Let's run this
and looking at the output,
you can see the directories were deleted.
And now if we run this each time, we should get consistently the same results.
And you can see that is the case.
You can see that the Path used, as well as some method used,
have different effects and results for each operation.
The createFile method; will throw an exception if the Path contains directories that do not exist;
it will throw an exception that the file exists unless the option
REPLACE_EXISTING is used;
a file will always be created even if the leaf element seems to be a directory reference
without a file extension, for example.
Moving on the delete method; will delete only the leaf element on the Path instance;
if the file or directory does not exist, an exception will be thrown;
use deleteIfExists will not throw an exception;
and a directory will only be deleted if it's empty.
Moving on to the createDirectory method, that will throw an exception
if the Path contains directories other than the leaf element that do not exist;
it will create a directory regardless
if the leaf element has an extension and seems to be referencing a file for example.
And finally, the createDirectories method;
will create directories for the entire path, if they do not exist;
will create a directory regardless if the leaf element has an extension
and seems to be referencing a file for example.
All right. So let's end the video here. And in the next one,
I want to look at move and copy operations. See you in the next video.