- [Tim] So we ended the last video talking about
deleting files, but at this point now
you might be wondering about creating files.
We haven't actually really done that at the moment.
So the files class does have methods for creating files
and directories, but do keep in mind
that you're usually creating a file
because you want to write something to it.
So, more often than not, you want to open a stream
or a channel, and doing that will actually create the file.
But with that said, let's look at how to use
the files class to do it anyway.
Now we're gonna create a file here called file2.txt
in the Examples directory.
So I'm just gonna copy, or comment out rather,
this code, and actually what I'll do is
I'll comment that out,
I'm gonna delete this old code from the previous video.
All right, so to create a file,
Path fileToCreate is equal to FileSystems
dot getDefault dot getPath,
and we're going to use the Examples directory
as I mentioned, and the file name,
let's go with file2.txt.
We can see at the moment looking at the directory
structure there, that there's not a file2.txt
in the Examples folder.
Now there's a file1.txt in that folder,
and obviously file2.txt in other subfolders
from the Examples folder, but not actually
in the Examples folder.
All right, and then to create it we just do
Files dot createFile fileToCreate.
Pretty straightforward, so let's just run that.
And you can see we've now got a file2.txt
showing up in the Examples directory.
And if you double-click it, you can see that
no surprises there, it's actually empty,
and if you want to write anything to it,
you'd have to create a stream or a channel,
so again, there's probably no point explicitly
creating the file using the createFile method,
but it is there if for some reason you
want to create it that way.
And the createFile method is an exception to the rule,
in that you can't use it to create directories.
So instead, let's actually create a Dir4 directory
under Examples using the method you need to create
directories, which is the createDirectory method.
So we're gonna do Path DirToCreate is equal to
FileSystems dot getDefault dot getPath Examples,
and then Dir4,
then we just do a call to the Files.createDirectory method,
actually createDirectory, which is a single method,
a single directory, I should say,
and that should be in lower case, just change that,
and dirToCreate.
And actually what I'll do now is I'll comment out
the fileToCreate because that's already been created.
And let's just run this.
Now we can see our Dir4 folder got created over there
as a subfolder of the Examples folder.
All right, now let's just try taking this
to the next level, though.
Let's try creating a directory called Dir6
that has the following path.
So Examples/Dir2/Dir3/Dir4/Dir5/Dir6.
Now I can probably hear you groaning,
you're thinking you're gonna have to
call createDirectory multiple times.
Now you could do it like that,
but there's an easier way, and you probably have got
a hint of that when you saw what I was typing
when I typed createDirectory.
There is actually a createDirectories command
or method I should say,
so let's just see what that looks like.
Path dirToCreate equals FileSystems dot getDefault
dot getPath Examples,
and let me type in the path,
it's gonna be, in the case of a Mac,
it's gonna be Dir2/Dir3/Dir4/Dir5/Dir6.
Let's just run that and then I'll give you
the equivalent command for Windows.
Files.createDirectories dirToCreate.
Let's run that.
And you can see we've got Dir 2, Dir3, Dir4, Dir5
and even Dir6, so pretty cool that this particular method
not only creates the last entry,
the last path the Dir6 in this case,
but it also creates any non-existent parent directories
as well, and basically all in a line of code.
Now if you're running on a PC,
you would do something like, I'll just comment this out
'cause I'm not.
Instead of the forward slashes,
you'd put two double slashes for each one.
Something like that.
So that's an equivalent thing on a PC,
so again we're escaping the backslash there
as we've done for all paths in this section of the course.
So that was one way of doing it.
The other alternative we could have done,
it was also something along these lines.
I'll comment that one out now.
We could have actually done Path dirToCreate equals
FileSystems dot getDefault dot getPath,
and we could put Examples,
in my case on a Mac /Dir2/Dir3/Dir4/Dir5/Dir6,
let's add one more now, seven.
So that will equally work well,
and actually if we try running that to confirm that it works
we should have a Dir7 now.
And you can see we've not got Dir7,
so that works as well, putting it all in one line like that,
which is pretty cool, and again, and on a PC,
change the forward slashes to backslashes,
and you'll get the equivalent result on a Windows machine.
All right, so the last thing that we're gonna look at
in this video, is how to get a file's attributes,
things like the file size when it was last modified,
whether a path points to a file or directory, et cetera.
And this data is also referred to as the file's metadata.
So file attributes or file's metadata.
Now using the files class you can get the value
of a single attribute or alternatively,
you can retrieve all the attributes at once.
So let's actually look at getting
a single attribute, the file size.
So we're going to just comment out that code now.
And let's try and grab the file size for the
Examples Dir1/file1.txt.
So it'll be Path filePath is equal to
fileSystems dot getDefault dot getPath Examples,
and the second argument is gonna be Dir1
forward slash because I'm on a Mac,
two backslashes if you're on a PC,
file1.txt.
So we're pointing to whatever file that we want to
get the attributes from, then we do a
long size is equal to Files dot size filePath.
And then let's just print that out.
I'll put Size is equal to plus size.
Let's just try running that.
Size equals 151 as you can see there.
And again on Windows, two backslashes to do the same thing.
All right, so that's the size.
Let's try another one, let's try the last modified date.
Let's do that as a shortcut, so I'm going to
do a printout here, and we'll put
Last modified equals plus, then we're gonna do
Files dot getLastModifiedTime filePath.
Run that.
And you can see we've got a time that
that particular file was last modified.
Noting that it contains both the date and time.
Now you may have to adjust that time for your time zone.
Now we're not gonna cover the code to do that
since it's really off topic,
but there's certainly lots of good information
on the internet for that if you want to do that
to change the time zone to whatever is appropriate
for your part of the world.
Now you can also check the file's class
to see what other methods are available
for getting the value of a single attribute.
So let's just quickly go and open the documentation.
Okay, the link's in the resources section
that I've just pasted.
And you can see that if we scroll down,
getOwner, getLastModifiedTime, which you've just seen,
getFileStore, FileAttributes,
isWritable, isSameFile, there's also
lots of other ones there that you can use,
so check that out if you want to find out a little bit more.
But the other thing you can do
is you can get all the attributes in a single shot.
To do that, you'll retrieve an instance of a class
that implements an interface containing methods
that can retrieve each individual attribute.
Let's go and have a look at how to do that.
And it's actually the basic file attribute interface
that covers a basic set of attributes
that are common to all operating systems.
So we're gonna use it here to get the attributes
for the same file, and consequently we'll be using
the same file path.
So to do that we're gonna put BasicFileAttributes
attributes is equal to Files dot ReadAttributes filePath,
and then the second argument is BasicFileAttributes.class.
And once we've got that,
we can start printing out some things,
so start off with size equals plus attributes dot size,
Last modified equal to, plus attrs,
abbreviation for attributes dot LastModifiedTime.
Just try a few more,
Created equals plus attrs dot CreationTime.
Do a couple more.
Is directory equals plus attributes dot isDirectory.
And we'll do one more, is it a regular file.
Is regular file equals plus attrs dot isRegularFile.
And if you run that,
we can see we've got our outputs there.
In this case that file correctly says it's not a directory
but it is a regular file, 'cause of course we're
pointing to file1.txt.
So the important part is this readAttributes method
that we're calling from the Files class
to get the basic set of attributes.
So we tell the method which sets you want to retrieve
by this second argument, BasicFileAttributes.class
in this case, the readAttributes method returns an instance
that implements the basic file attributes interface,
and you have to actually pass the BasicFileAttributes.class
because there are system-specific sub-interfaces
of basic file attributes in the java.nio package,
that can also be retrieved using this method.
So you can then call methods as you can see below that
on lines 33 through 37, to get the values
for the attributes that you're interested in.
Now in addition to getting the size and last modified time,
you can see here we're also getting the creation time stamp,
whether the path points to a directory,
and whether it points to a file.
And this is only a very small subset of what's available
for the BasicFileAttributes.class.
If you go back to the documentation,
and check that out, there's the link
and it's in the resources section,
and you can find out the other methods
that you can get access to,
to find out more information if needed.
Now java.nio contains other interfaces for file attributes
by the way, that are operating system specific,
in that some of the attributes they retrieve
are only available on the operating system they target.
All right, so let's finish the video here now.
In the next video we're gonna continue our look,
looking at the file system using java.nio.
See you in the next video.