Java básico 19 - Gestión de archivos en Java - II — Transcript
Full transcript
- 0:02Perfect. So,
- 0:05uh, let's open our IDE.
- 0:12Very good.
- 0:14So here we are, right where we left off
- 0:16, which was reading the contents of a
- 0:18file.
- 0:20We saw the process of how we did it, you
- 0:22did the homework, and so on.
- 0:27So, uh, today we are going to continue
- 0:30with, uh, how to copy a file.
- 0:33So, I am going to remove everything that
- 0:35is here.
- 0:39Let's work with a cleaner file. You
- 0:41already have all of this in the other
- 0:43video just in case
- 0:47so that everything remains clear.
- 0:51Very good,
- 0:56there we have our file that was already
- 0:58created from the previous video, the
- 1:00file, and we will work on this one.
- 1:03Uh, I am going to run the program to see
- 1:05that it doesn't have any kind of error.
- 1:11It is compiling there.
- 1:17Perfect. There it is. File handling in
- 1:20Java.
- 1:21There is no error. So we are going to
- 1:23work with
- 1:25uh
- 1:28we are going to be working with
- 1:33copying
- 1:37a file.
- 1:39That is what we will be doing now.
- 1:43So we, uh, can copy the contents of one
- 1:47file to another using one of the classes
- 1:50that come when we install the Java
- 1:53Development Kit.
- 1:56Those classes are called FileInputStream
- 2:00and FileOutputStream.
- 2:04Using, I'm going to repeat, we are going
- 2:07to use the classes, uh, file
- 2:11input,
- 2:14that one you see on screen,
- 2:15FileInputStream.
- 2:18And we are going to use the other one
- 2:22called file
- 2:25output
- 2:32file this one right here
- 2:38stream.
- 2:39Exactly. So, using the FileInputStream
- 2:43class, we can read the content of the
- 2:47source file.
- 2:50Yes, with this class,
- 2:54uh, we can read the content of the
- 2:57source file and, using the other class,
- 3:00the file input, sorry, the file output,
- 3:03this machine that we are going to use to
- 3:06write our, our other file, that is, the
- 3:09copy that we are going to make, we are
- 3:12going to use the class file
- 3:17OutputStream, this one right here.
- 3:20So, with this we read the file.
- 3:24Let's enlarge it a bit so you can see it
- 3:26better.
- 3:28With this, uh, we read the file that we
- 3:31want to copy and with the file stream,
- 3:34uh, we send it to be written to another
- 3:37file.
- 3:43So, let's see how we can implement those
- 3:46functionalities.
- 3:53So,
- 3:58the first thing we have to do is
- 4:01come over here. We are going to import.
- 4:07Notice that I am importing the Java
- 4:10package.
- 4:13and an asterisk. This asterisk what it
- 4:16indicates to the program is, look, load
- 4:19all the classes that are inside the java
- 4:22package.
- 4:25For example, so you can see the
- 4:27difference, I can say, "Okay, let's work
- 4:30with those two classes."
- 4:32So, I can tell it, uh, import Java,
- 4:35right?
- 4:37IO, I type a dot and it shows me all the
- 4:41classes available for IO.
- 4:45In this case, io means input output.
- 4:50So I can tell it, look, I am going to
- 4:51use the file class.
- 4:56input stream, this one right here, we
- 4:57are going to use it and I select it.
- 5:01And I am also going to say that we will
- 5:03use
- 5:07the dot file output, excuse me, file
- 5:10output stream.
- 5:13Yes.
- 5:15And there we have the two classes
- 5:19imported to be able to work with our
- 5:21functions that we are going to create.
- 5:25If we don't do the imports, then it
- 5:27won't work.
- 5:30Usually in these advanced editors, when
- 5:34we make use of these classes, the editor
- 5:37automatically adds these two imports to
- 5:41the class, to our project.
- 5:46Yes, but if we want to use, let's say,
- 5:49all the IO classes, then we would simply
- 5:52have to say import java.io
- 5:57asterisk and it would load all the
- 6:00others, it would load all that are
- 6:03contained there.
- 6:06Do you see?
- 6:09So, uh
- 6:13since we have been working with
- 6:16different classes to create files, to
- 6:18read, uh, and we are also going to
- 6:21delete, then the ideal thing would be to
- 6:24use io.
- 6:25So that it imports all the ones we are
- 6:28going to use.
- 6:29Well, so you know, we can do it
- 6:32individually.
- 6:35If we are only going to work with these
- 6:37two, we import them, but since we are
- 6:39working with file management in all its
- 6:41aspects, let's import them all.
- 6:44So, I am going to remove this from here
- 6:46and I will stick with this.
- 6:48That is what the asterisk means and it
- 6:51applies to any other import that you may
- 6:53have.
- 7:18Well,
- 7:19so let's work B
- 7:52Well, so let's get started.
- 8:02So here
- 8:06I am going to remove this.
- 8:08Let's do it all, remove all of this.
- 8:11Yes.
- 8:16So,
- 8:19the first thing we are going to do is a
- 8:21try.
- 8:23Everyone already knows what the try is
- 8:24used for.
- 8:25I explained that in the previous video,
- 8:28so I won't emphasize it too much.
- 8:32I am going to define a variable of type
- 8:34byte.
- 8:36Yes,
- 8:38this is going to be an array. Notice
- 8:40that it is an array of type byte
- 8:43and I am going to call it, uh, data and
- 8:47this will be equal to a new
- 8:53let's put it.
- 8:59So we are going to create, uh,
- 9:11I will complete that now with the catch
- 9:13. It is asking me for a catch to trap.
- 9:23We are going to fix that now. So, uh,
- 9:28we have a char,
- 9:31yes? Let's say this one will be called,
- 9:34let's say, uh,
- 9:38let's call it x, for example, a variable
- 9:40.
- 9:41Let's say that this will be equal to new
- 9:43, uh, char.
- 9:46Let's set it to 100, equal to 100.
- 9:52So let's
- 9:55define, uh,
- 9:58our our reading file,
- 10:03file,
- 10:06define a file input stream object.
- 10:10This object, we're going to call it, uh
- 10:11,
- 10:18let's call it input file, for example,
- 10:22input file.
- 10:26And this is going to be equal to a new
- 10:28file input
- 10:31input stream.
- 10:33Yes. And there we are going to place, uh
- 10:35,
- 10:38a name. Let's name it, uh,
- 10:42let's name it new
- 10:46new
- 10:48new file.
- 10:50New file dot txt.
- 10:54This goes in quotes.
- 10:58That's right.
- 11:03Let's continue.
- 11:06Uh, let's define the output file now.
- 11:10F output stream.
- 11:14Let's give a name. To this. We're going
- 11:16to we're going to call it
- 11:18output file.
- 11:21It will be equal to a new file stream.
- 11:26Here we are going to place
- 11:29the,
- 11:31let's call it
- 11:35destination file, for example,
- 11:40semicolon.
- 11:44So, uh,
- 11:51we're going to here we're going to
- 11:54invoke the file, sorry, we're going to
- 11:56invoke the
- 11:59the file name, the input file.
- 12:05to read read
- 12:13to read data or data, sorry, since we
- 12:16called it data
- 12:20and we're going to tell our output file
- 12:25to write here and we're going to pass it
- 12:33the data.
- 12:34Yes,
- 12:37let's put a little sign here.
- 12:44Let's put data
- 12:48contained in the file
- 12:59or say
- 13:01uh
- 13:06the data contained in the in the file
- 13:08have been copied.
- 13:10Have been copied.
- 13:12Yes,
- 13:15it would be
- 13:24have been copied. So, then what we must
- 13:27proceed to do is close the file.
- 13:39Yes, we are going to close the file. Uh
- 13:42,
- 13:50input file dot
- 13:58close. We're going to close it
- 14:01and we are also going to close the
- 14:04output file
- 14:08close. We are going to close it.
- 14:14Yes.
- 14:18So, uh,
- 14:24here we are going to place another
- 14:26message
- 14:30to this.
- 14:35Let's tell it, uh,
- 14:40showing data from the destination file,
- 14:46for example.
- 14:49Yes, we're going to do that now.
- 14:58So, what we have to do now is a file
- 15:01reader. We already saw this in the last
- 15:03class. File reader.
- 15:07File reader, we give it a name. FR, will
- 15:10be equal to a new, uh, file reader.
- 15:15It's a new file reader.
- 15:17And we are going to assign it a name.
- 15:19The name we already gave it. Ah, this
- 15:21was this was the name we gave it.
- 15:24Destination file.
- 15:29We are going to copy this here.
- 15:33That's right.
- 15:38So here we are going to do a try too,
- 15:41we're going to try tr.
- 15:47Yes, we're going to try to to read file
- 15:51read. We are going to read that file.
- 15:55Yes.
- 16:06So, we're going to read it. Here it is
- 16:08in the
- 16:10we use the X. Remember the definition of
- 16:13the X. Here we're going to read then
- 16:16here X. Yes.
- 16:20and
- 16:22we are going to
- 16:24print
- 16:28the content of X. That's right.
- 16:32So we are going to do a catch.
- 16:34We are going to catch
- 16:37in case an exception is thrown.
- 16:39Exception.
- 16:41We'll call it E
- 16:46and we are going to print, in case an
- 16:47error occurs, we are going to
- 16:51print the stack trace
- 16:54of
- 16:59finally we are going to
- 17:02close the file we are reading.
- 17:06Yes.
- 17:16And we still need to handle the
- 17:18exception, this exception here, that we
- 17:20are working on, this one right here.
- 17:24So there we are going to do the same.
- 17:26Exception
- 17:29e
- 17:32and we will print, in case an error
- 17:35occurs, we will print the stack trace
- 17:39here.
- 17:42So
- 17:44I think
- 17:46that's all we need.
- 17:51Yes, nothing else to do. Let's see
- 18:05let's check the error it has. Let's see
- 18:06, it says this error.
- 18:13It is written here. This is char.
- 18:17Correct.
- 18:19Now it's fine.
- 18:23Let's run this program to see what
- 18:27it will do.
- 18:29Good afternoon.
- 18:33Let's see how this program works.
- 18:40Let's see.
- 18:43Exactly. Notice something,
- 18:47it's giving me an error because it says
- 18:50new file obviously doesn't exist.
- 18:53Right?
- 18:55So we have to put here
- 18:59the file, a file that exists.
- 19:04I suspected this would give me an error
- 19:06.
- 19:07So, look at what is happening. This is
- 19:08giving me an error for the following
- 19:09reason.
- 19:10I am trying to open the file that I am
- 19:13trying to copy and it is called new file
- 19:15, but that new file doesn't actually
- 19:18exist.
- 19:20So, when I run the program, when it
- 19:22tries to read this, the error is
- 19:24triggered.
- 19:27Since that exception is not caught, it
- 19:28displays it for me immediately here.
- 19:31And notice it tells me the exception
- 19:33being triggered is the file not found
- 19:35exception, meaning the file not found
- 19:37exception, and it tells me the name it
- 19:39is not finding.
- 19:41So to solve this, what I am going to
- 19:43give you is this file here, which we
- 19:45created in the previous class, which is
- 19:47called archivo from Java dot txt.
- 19:51So, here where it says new file, I am
- 19:53going to put a filename that I know
- 19:56exists.
- 19:57Look at it, here it is called archivo
- 20:03from
- 20:05Java. That was the one we made in the
- 20:07other class.
- 20:08Okay, so with this we will be able to
- 20:11work
- 20:14correctly. It will try the input file.
- 20:19Since it will find it, the error will
- 20:21not occur. Let's see now.
- 20:24Let's go back to
- 20:27run this. I'm going to clear the console
- 20:30and let's run it again.
- 20:42Good. So, look, it tells me the file, it
- 20:45can't find it. So, where is the problem
- 20:48?
- 20:48It's that I made a typo, meaning, I
- 20:50spelled the name wrong.
- 20:52Look right here where it's misspelled.
- 20:54Archiv. It was missing something here.
- 20:56So,
- 20:58archivo.
- 21:01Now yes, archivo.
- 21:05It's missing the R.
- 21:08Archivo
- 21:10from Java.
- 21:12from quit. Now it is, it's spelled
- 21:14correctly.
- 21:35Let's see. Now we are running it.
- 21:39And now yes, look,
- 21:43it read the file called archivo from
- 21:45Java TXT and created a new one with the
- 21:48data that was contained in archivo from
- 21:50Java.
- 21:52It created a new file called
- 21:54archivodestino.txt and then I sent it to
- 21:57display the data it found.
- 21:59Look there where it's displaying it.
- 22:02In fact, notice that now within the
- 22:05project, both files are shown.
- 22:08Archivo from Java TXT.
- 22:12Look at the content here and the
- 22:14destination one here, it copied it again
- 22:17.
- 22:18Yes.
- 22:26Very well, let's close this.
- 22:34Any doubts, questions? raising your hand
- 22:36.
- 22:43This is what we have done.
- 22:46We simply defined
- 22:50uh
- 22:52a byte-type variable called datos.
- 22:55It is an array. Then we defined another
- 22:57array. What would it read? We named it X
- 23:00, in this case a name, uh, and created a
- 23:03new char of 100.
- 23:05So, then we called
- 23:09the file from Java, which is the one we
- 23:12had saved, and we defined an output file
- 23:15that will be called archivo destino.
- 23:21We read the data uh and pass it to datos
- 23:26.
- 23:28Meaning, the reading we do from the file
- 23:30, archivo from Java, we store in datos
- 23:33and then output file writes datos.
- 23:38Finally, uh
- 23:42we show the message that the data from
- 23:45the file
- 23:48from Java TXT has been stored
- 23:53or copied.
- 23:55We close both files and then we proceed
- 23:57to show the data in the new file that
- 24:00was created.
- 24:02For that, we create a new file reader
- 24:05object, pass it the newly created file,
- 24:08and attempt to read it.
- 24:15Right, so with that we can
- 24:20copy a file that already exists, copy it
- 24:22to another.
- 24:28So, uh
- 24:30we are going to work now on deleting a
- 24:33file.
- 24:34We already created a new one. Now we
- 24:36will see how we can
- 24:40delete it.
- 24:41Right, so
- 25:02I am going to clear all this.
- 25:06This time I'm not going to comment it
- 25:07out, I'm going to delete it.
- 25:10Yes.
- 25:11I am going to remove all this that is
- 25:12here.
- 25:19Yes, there we are back to how we were.
- 25:24So, what do we do uh to physically
- 25:28delete a file?
- 25:31For that, we have to invoke the method
- 25:34uh which is the delete method of the
- 25:38Java class.
- 25:40Let's see how we can implement this.
- 25:47For example, I can say the following.
- 25:52I can, uh, a file, file object.
- 25:55I am going to define an object.
- 25:58Let's name it
- 26:02file.
- 26:06That file will be of type new file.
- 26:11Yes?
- 26:12So there we have to specify the file
- 26:15name.
- 26:18And in this case, we are going to delete
- 26:20the file we just created. File
- 26:26destfile
- 26:28dot txt.
- 26:33Look at it here. This is the file we are
- 26:35going to try to delete. So, uh, I define
- 26:38an object called file.
- 26:41You can give it any other name you want
- 26:43, file, this type is file, and we pass
- 26:45it the name of the file we wish to
- 26:47delete.
- 26:50Then
- 26:54we are going to create a variable here
- 26:56of type boolean
- 27:00and we are going to say here, uh,
- 27:04file deleted.
- 27:08Meaning, file deleted will be equal
- 27:13to
- 27:15file, which is the object we just
- 27:17created.
- 27:18Option to put here
- 27:21file.
- 27:25File
- 27:27dot
- 27:30to invoke the method, and we look for
- 27:32the delete method. Look at it there.
- 27:35The delete method, when it is executed,
- 27:40uh, returns a boolean, which means that
- 27:43if the operation of deleting the file
- 27:45was executed correctly or successfully,
- 27:48then it returns a true.
- 27:51And that value it returns, I am storing
- 27:54it in the boolean variable file deleted
- 27:56.
- 27:57Yes?
- 28:00So here I can put a conditional.
- 28:03There are several ways to do this, but I
- 28:05am going to do it in the simplest way. I
- 28:07am going to say if, I am going to ask a
- 28:09question.
- 28:12If file deleted
- 28:16if it is true.
- 28:18Then here I am going to output a message
- 28:20, a small message that says
- 28:23uh
- 28:26the file has been deleted
- 28:30successfully.
- 28:34It has been deleted. All right.
- 28:36Been deleted.
- 28:41If we want, we can add an else.
- 28:43Otherwise,
- 28:46we can say
- 28:54the file could not be deleted,
- 29:02so to speak.
- 29:04Yes.
- 29:12And that's it. That is all we have to do
- 29:14. Notice, when we run this process, this
- 29:17program, it will simply do is, uh,
- 29:20create an object of type file, and to
- 29:23that file, the physical file we have
- 29:26created, destfile.txt, will be assigned
- 29:30, and it will try to delete it.
- 29:33If it deletes it, that method will
- 29:35return a boolean indicating true if it
- 29:37deleted it, or false if it did not
- 29:38succeed.
- 29:40Then that value it returns is stored in
- 29:42file deleted.
- 29:44Finally, I send to display on the screen
- 29:46whether the file was deleted or not.
- 29:50Let's run this and see how it goes.
- 29:58And there it is. Look, the file has been
- 30:01deleted. If you notice, it's already
- 30:06the file we had, which was called
- 30:08destination file, is no longer in the
- 30:10folder, which means
- 30:14that the procedure was carried out
- 30:16correctly. It has deleted the file, um,
- 30:22destination.txt.
- 30:30Perhaps you could place, um,
- 30:37a question, um, asking if you really
- 30:40want to delete the file,
- 30:44and depending on what the user answers—
- 30:46this is just to talk, for example—you
- 30:47would have to capture input via the
- 30:49console asking if they wish to delete
- 30:50the file.
- 30:52If they answer yes, then proceed to
- 30:53execute the deletion method.
- 30:56If they answer no, then you could output
- 30:59a comment saying that the process of
- 31:01deleting the file has been canceled.
- 31:14So, um,
- 31:19it is quite straightforward, there is no
- 31:21need to overcomplicate this.
- 31:23So, let's see now, um,
- 31:29permissions in Java.
- 31:34What do we mean here by permissions in
- 31:37Java?
- 31:40We can verify file permissions using, um
- 31:44, different methods present in the Java
- 31:48file classes.
- 31:52What I am going to show you now is an
- 31:54example, um,
- 31:57to verify if the file we are trying, um
- 32:01, to use has read, write, and execute
- 32:05permissions.
- 32:08Let's see how we do that.
- 32:16So, I am going to remove all of this
- 32:21here.
- 32:23I will leave the label "file handling."
- 32:25I am going to remove all of this.
- 32:28Well, we can leave this one for the new
- 32:30file. We are going to use—we are not
- 32:32going to use, um, destination file
- 32:35because it no longer exists; we deleted
- 32:37it. Let's give it the other name since
- 32:40we have a file there that is still there
- 32:42. File
- 32:48file
- 32:50from.
- 32:55So now we can remove this because we are
- 32:57not going to delete it.
- 33:02Let's remove this.
- 33:04So, um,
- 33:08let's place a few comments here.
- 33:15Let's put, um,
- 33:21permissions
- 33:23available.
- 33:27Permissions available for
- 33:33here we are going to place
- 33:37um, file, right? Let's put file.
- 33:43Let's look for a method that displays
- 33:45the name for me.
- 33:47Let's see if we have, um, I think I have
- 33:50"get name" to get the file name.
- 33:54Let's test it immediately.
- 33:58If this is working. Let's run the
- 34:00program.
- 34:05Indeed,
- 34:10look at the file here, permissions
- 34:13available for file TXT.
- 34:17In fact, I am going to place it, um,
- 34:21let's put it better like this, so it
- 34:22looks a little better.
- 34:24Let's remove this from here.
- 34:28Yes.
- 34:31And below we are going to print another
- 34:34label. Yes.
- 34:37And here we are going to place "
- 34:38permissions available for"
- 34:45the file.
- 34:48And now we are going to place
- 34:54the method.
- 34:57What this method does is return the name
- 34:59.
- 35:00We are going to place
- 35:03some small—we are going to create a kind
- 35:05of
- 35:07of output.
- 35:09Yes, let's copy this here as well to
- 35:12give it a little bit of formatting, so
- 35:15to speak.
- 35:24Yes, I think that
- 35:26is fine.
- 35:31Very well.
- 35:33Let's test this. Let's test how the code
- 35:36is going.
- 35:40Yes, look, we created the
- 35:44effect I wanted.
- 35:47It needs to be separated a bit. Let's
- 35:48adjust it here.
- 35:51Let's add a space so it stays
- 35:56let's put some arrows here. Like this.
- 36:00Let's run this again.
- 36:06Perfect. Much better. There is the name
- 36:08. It's displayed now.
- 36:11If we wanted, we could center this a bit
- 36:13more, I don't know, to
- 36:17give it a bit of shape.
- 36:20handling.
- 36:23Let's see.
- 36:29Oh yes, much better.
- 36:33More.
- 36:39Perfect.
- 36:40So, we already have our sign, file
- 36:41handling, the permissions available for
- 36:43the sign.
- 36:45So, let's see how we can display on the
- 36:47screen what permission the file has.
- 36:50So,
- 36:53we are simply going to display a few
- 36:55signs.
- 36:56Here we are going to write, for example
- 36:58,
- 37:02let's check for execution.
- 37:11execution. If it has execution
- 37:13permission.
- 37:17Yes.
- 37:35That's right. Execution permission.
- 37:38And there we will display it.
- 37:44Let's tell the file to tell me, let's
- 37:47look for the method that allows me to
- 37:49show if it has permission.
- 37:52Let's look for a method called execute.
- 37:56Look at it there.
- 37:58Can execute. Look at it there. If it can
- 38:00be executed. Can execute.
- 38:03It is a method that returns a boolean.
- 38:06Yes,
- 38:09we already have the first one.
- 38:12Let's copy this same thing here.
- 38:16And now we will call this one reading.
- 38:19We will make it readable, so to speak.
- 38:24Reading. Let's put reading. I think it's
- 38:26better understood. Reading.
- 38:29Put execution permission.
- 38:32I think it would look better as reading
- 38:33.
- 38:34Let's see if it has read permission.
- 38:37How could we see that? We simply change
- 38:39the method instead of can execute.
- 38:43Let's look for the one that allows me to
- 38:44show that. So we are going to say can
- 38:47can can can can read. Look at it. Here
- 38:51I select it.
- 38:55What else could we apply there?
- 38:58Let's see if we have write permission.
- 39:01It is the same process.
- 39:03System.
- 39:04Println.
- 39:07Yes.
- 39:09Tell it there write.
- 39:13Write.
- 39:15That's right.
- 39:18We have it.
- 39:22And the only thing we have to do now is
- 39:24invoke.
- 39:31That's right here file dot
- 39:37we are looking for if we can write. So
- 39:40it would be can write. Look at it here.
- 39:47And there it is.
- 39:48So, this should display on the screen.
- 39:53Let's draw a line underneath so that
- 40:02here we are going to change it, we are
- 40:04going to put
- 40:07shape.
- 40:14Let's see how this looks.
- 40:21We are running it, compiling to see if
- 40:22there are any errors.
- 40:24And look,
- 40:27it tells me that I have execute, read,
- 40:31and write permissions.
- 40:35Let's see if I can change
- 40:38the permissions. Let's see.
- 40:43I would have to go to the folder.
- 40:50Let's see.
- 40:58C: users user. Okay. Let's see. I'm
- 41:01going to go to the folder.
- 41:09Just a second, I'm going to try to
- 41:11change
- 41:14users user,
- 41:17IntelliJ Projects,
- 41:24Java Basic, the file from Java. I'm
- 41:28going to try to change the permission.
- 41:31I'm going to properties. Let's see.
- 41:35Okay. And I'm going to set it here to be
- 41:39read-only.
- 41:44I'm going to click save.
- 41:46Okay.
- 41:48Now I'm going to run my program again to
- 41:50see what it says.
- 41:55Let's run it.
- 41:59Look,
- 42:01now the file says execute. true for read
- 42:05, true for write, false. Which means
- 42:10that I cannot modify this file because
- 42:13it is false.
- 42:16I cannot write to it because, through
- 42:18the operating system, I have told that
- 42:21file that it is read-only, which is why
- 42:23I cannot modify it.
- 42:27So, if I go back to the folder, select
- 42:30the file again, and right-click,
- 42:33properties,
- 42:35you can't see it.
- 42:37That's correct, student, you can't see
- 42:39it.
- 42:39But you are supposed to know at this
- 42:43stage of the game how to set a file's
- 42:47properties.
- 42:49This is operating systems. I mean, I
- 42:51just go to a folder, it's as simple as
- 42:53that. That's why I haven't shared it,
- 42:56but well, I'm going to
- 43:01It doesn't hurt. I'm going to stop this
- 43:03here.
- 43:04I'm going to share my window here.
- 43:09Now you can see it, student. Yes,
- 43:15you can see it.
- 43:16Yes, it's visible.
- 43:18Perfect. So, look, there is the file.
- 43:22So, I select it, right-click, properties
- 43:25.
- 43:26Do you see it?
- 43:28It shows me the file properties. Notice
- 43:31where it says attributes read-only,
- 43:33hidden.
- 43:34There are others here that you can
- 43:36define.
- 43:38In this case, I'm only going to tell it
- 43:39what it will be.
- 43:42I uncheck the box that says read-only so
- 43:44that it can be overwritten.
- 43:46When it is checked, it means that it is
- 43:49read-only, that I cannot modify it.
- 43:52So, I uncheck it.
- 43:54Now it will be available for read and
- 43:58write.
- 44:01If we want to apply more, we can go to
- 44:02advanced
- 44:04and there we see other options.
- 44:07But for the example we are doing, if I
- 44:10click apply,
- 44:13right? So I'm going to stop sharing here
- 44:16. I'm going to share the project again.
- 44:20Yes,
- 44:23you are seeing the code again there. The
- 44:25file is already there. I removed the
- 44:27read-only condition.
- 44:30If I run the program, then for writing,
- 44:32notice how it tells me now.
- 44:35Writable true, meaning that I can modify
- 44:40that file.
- 44:44and I put it back on.
- 44:55If I go back and set my file
- 45:00in properties to be read-only, meaning I
- 45:02cannot modify it.
- 45:08I'll stop this here. Here I share the
- 45:11code again.
- 45:13I have put the code back to read-only,
- 45:15the read-only attribute.
- 45:18If I run the program now, look at what
- 45:20it will tell me at the end.
- 45:23It says false because I set it to be
- 45:26read-only.
- 45:28Good,
- 45:31I think this is quite clear, quite easy
- 45:33to apply.
- 45:36I'm going to set this back to how it was
- 45:37.
- 45:39Okay, ready.
- 45:43So, uh, let's see now finally
- 45:51how we can list files in the directory,
- 45:54how we can show all the files we have
- 45:57from a Java program.
- 46:03Uh, with this example I'm going to show
- 46:06you now, we are going to create a new
- 46:08directory using the MKDIR method.
- 46:11That is an operating system command, and
- 46:14we are going to create some new files
- 46:17inside that directory using the class
- 46:20you already know called createNewFile.
- 46:27and then we will be able to list the
- 46:29file contents using the list method.
- 46:33So let's
- 46:37work with that.
- 46:43For now, I'm going to need to take the
- 46:50address of the folder where I want to
- 46:52work. I already have it copied here.
- 46:56So let's get to work.
- 47:04So, uh
- 47:08we already have this execution ready.
- 47:10This I'm going to
- 47:13I'm going to remove.
- 47:18Remember, this will be recorded, you
- 47:20will be able to watch it again calmly
- 47:22and do all these steps again.
- 47:26So, uh, I'm going to place the path here
- 47:29in a comment so you can see where it is
- 47:31.
- 47:32That is the path where we are working.
- 47:35So, uh, let's see how we do this.
- 47:51Well, so let's go.
- 47:53The first thing I'm going to do is
- 47:56create a file, a new file.
- 47:58You already know what this command means
- 48:00. I'm going to give it a name so it
- 48:03looks easier to understand. An object
- 48:07called F of type file. I could have
- 48:10given it, uh, any other name. For
- 48:13example, let's call it new file, for
- 48:16instance.
- 48:19Yes, new file.
- 48:22The object. New file.
- 48:26Yes.
- 48:27And new file.
- 48:29Will be equal. Please, mute your
- 48:31microphones.
- 48:33It's just like a new file.
- 48:38F.
- 48:44In fact, let's call it,
- 48:48yes, let's leave it as file.
- 48:55Let's give that new file a name, let's
- 48:57call it
- 48:59uh
- 49:02directory, for example.
- 49:11No, sorry, I'm doing something wrong.
- 49:19Well, yes, we can do it that way. New
- 49:20file. Let's call it, uh, directory.
- 49:28Directory
- 49:33what we
- 49:37are going to do is invoke that on the
- 49:39file called new file.
- 49:45Notice, we have assigned this name to
- 49:48the new file object.
- 49:52So, what we are going to do is create an
- 49:54if,
- 50:01let's put an else as well
- 50:04and here we will execute this. We are
- 50:06going to say that if
- 50:09new file dot and we are going to execute
- 50:11the command that allows me to create a
- 50:13directory called MKD.
- 50:16Look at it here.
- 50:20MKS.
- 50:22If this executes correctly, it returns a
- 50:25boolean, and since I'm asking if the new
- 50:28directory MKDIR method is true, it will
- 50:31indicate that a new directory has been
- 50:35created.
- 50:36We are executing this method that allows
- 50:39me to create a directory.
- 50:41Here I will place a little message.
- 50:46It says
- 50:48the directory
- 50:56directory has been created.
- 51:02Otherwise,
- 51:05let's copy this same thing. Practically
- 51:07the only thing it will say here
- 51:11I'll edit it here to
- 51:14let's do it like this.
- 51:17We will say, the directory, uh, could
- 51:20not be created,
- 51:24could not be created,
- 51:29be created.
- 51:32That's right. The directory could not be
- 51:34created.
- 51:41I can put this all in uppercase, uh,
- 51:42there's no problem with that.
- 51:45I'm just giving it a kind of style, you
- 51:47know,
- 51:55has been created. Then, otherwise, the
- 51:57file was not created.
- 52:03So
- 52:08we
- 52:11are going to
- 52:25In fact, let's test this to see if it
- 52:27works.
- 52:30I'm going to run it to see if it creates
- 52:32the directory for me. Let's see.
- 52:34I'm going to run the program.
- 52:42It's compiling.
- 52:45And notice the message it gives me.
- 52:47The directory has been created.
- 52:51If you look at the project, a new folder
- 52:54appears called directory.
- 52:56Look at it here.
- 52:58Do you see it? We have created
- 53:01the directory, the command created the
- 53:03directory inside the folder for me.
- 53:06If I, uh, have the share and I share,
- 53:12it seems I closed the folder.
- 53:14It's here,
- 53:17sorry. Here, where is it? Now I got lost
- 53:20.
- 53:22Share.
- 53:26Here it goes, basic. Share.
- 53:29You can see, look at the folder we
- 53:30created. Look at it there. We just
- 53:32created it right now. Look at it there
- 53:34with the MKD command.
- 53:37We already have the folder created.
- 53:40I have the path where it is. Look here,
- 53:42that is the path. I copied that and put
- 53:45it in a comment. So what we are going to
- 53:48do is send it to save inside that folder
- 53:50that says directory. We are going to
- 53:52save a few files.
- 53:53If I enter the folder, you will see that
- 53:55it is empty. This folder is empty.
- 53:59So, inside that folder we are going to
- 54:01place, uh,
- 54:05the new files. I am going to copy this
- 54:07path,
- 54:10I am going to stop sharing. I will share
- 54:13my code again and we are going to do it
- 54:17.
- 54:19Yes, we are going to do it.
- 54:30We are going to create a stream.
- 54:35We create a stream.
- 54:39We are going to say, uh,
- 54:42file path.
- 54:45Yes, file path will be equal.
- 54:49And there I am going to copy
- 54:52uh the path.
- 54:55Yes.
- 54:57And I am going to pass it
- 55:00the new name I am going to create. I am
- 55:02going to name it, for example,
- 55:09file
- 55:1201,
- 55:16file 01
- 55:21dot txt.
- 55:25Yes,
- 55:27let's create another one of these. I am
- 55:30going to copy this.
- 55:34This is showing me an error.
- 55:37Obviously, the parenthesis does not go
- 55:38there.
- 55:40And here I am going to copy another one
- 55:42. Let's go to file path.
- 55:46File path. Let's put 01 on it.
- 55:51And to this one we will put file path 02
- 55:53.
- 55:55A name. And here we will put another
- 55:57file name.
- 55:5802.
- 56:00This does not go.
- 56:03So,
- 56:06simply
- 56:10we are going to define now
- 56:15my two files.
- 56:19We already know how to do this. I define
- 56:21an object of type file, let's call it,
- 56:25uh,
- 56:27file 01
- 56:31is equal to a new, a new file.
- 56:36And that new file, what are we going to
- 56:38pass to it?
- 56:40Well, we are going to pass it the path
- 56:41we just created. What is the path?
- 56:44File path 01.
- 56:49We will do the same with the next one.
- 56:51We create an object of type file and
- 56:53assign a name. File 02 will be equal to
- 56:57a new file object.
- 57:02New file object.
- 57:06And what path are we going to pass it?
- 57:07We are going to pass it this file that
- 57:09is here.
- 57:15is.
- 57:19So, if all this works, it should have
- 57:22created my two files.
- 57:26Let's execute the program.
- 57:28Remember, the folder is empty.
- 57:32Let's execute the program to see what it
- 57:34shows me.
- 57:36I am going to click run.
- 57:41Let's wait.
- 57:44Perfect.
- 57:49It is showing the file. It could not, uh
- 57:52, it could not be created.
- 57:55Sorry.
- 57:58Let's see. The directory could not be
- 58:00created.
- 58:05Obviously, do you know why that is
- 58:07happening? because we already created
- 58:09the directory,
- 58:12but let's, let me enter the directory,
- 58:14let me see.
- 58:15Perfect.
- 58:18What I'm going to do is delete the
- 58:20directory because it's telling me it
- 58:22couldn't be created since the directory
- 58:24already existed.
- 58:27So, what I'm going to do is delete the
- 58:29directory folder.
- 58:31I'm, I'm inside even though you can't
- 58:33see it.
- 58:34I'm going to delete the directory.
- 58:37I just deleted it.
- 58:43Yes. Notice how now in the
- 58:47in the project the directory folder no
- 58:49longer appears. Yes.
- 58:53So I'm going to run my program again.
- 58:56This can also be validated. First, ask
- 58:58if the directory exists, and if it
- 59:01exists, then, uh, I would cancel the
- 59:04creation. But in this case, we'll keep
- 59:06it as is.
- 59:07I simply deleted the folder and I'm
- 59:10going to try running the program again.
- 59:15Let's see. Now I'm going to clear the
- 59:19console here.
- 59:21Let's run the program again. Let's see
- 59:23what happens.
- 59:25Notice now how it tells me the directory
- 59:28has been created.
- 59:30If you look at the project, the folder
- 59:34has been created again.
- 59:42That's right.
- 59:44And if I go to the
- 59:49directory folder,
- 59:54the folder is empty.
- 59:56Let's see. Uh,
- 1:00:02it didn't create the files for me. Let's
- 1:00:05see why.
- 1:00:19I think it has to do with
- 1:00:22with the specified path.
- 1:00:26I'm going to change this. I'm going to
- 1:00:28change, add the slash.
- 1:00:30I'm going to change this. Let's see.
- 1:00:36Yes, I think this.
- 1:00:47Let's see.
- 1:01:29Actually, I think it was fine. It didn't
- 1:01:32save the files because we are missing
- 1:01:33something. It's not the path. I'm going
- 1:01:35to put this back the way it was.
- 1:01:39Now I'm going to, I'm going to explain
- 1:01:41why it didn't save the files, and it's
- 1:01:42obvious.
- 1:01:45Fine, I'm simply reverting the path to
- 1:01:46how it was. I thought it was a problem
- 1:01:49with the path, but let's see if I'm
- 1:01:51wrong.
- 1:01:53Okay, there it is as it was before.
- 1:01:55What are we missing? It's that I created
- 1:01:58the files, but I'm not telling it to
- 1:01:59save them.
- 1:02:02Is that understood?
- 1:02:04So here I have to tell it,
- 1:02:10I have to tell it file 01
- 1:02:15create file. That was the error, why it
- 1:02:18wasn't creating it.
- 1:02:20We create everything, but I have to give
- 1:02:22it the command to create the file.
- 1:02:25And I have to do the same with file 02.
- 1:02:28Create file.
- 1:02:32Now it should work.
- 1:02:33Obviously, I have to delete this folder
- 1:02:35again, this directory folder you see on
- 1:02:37the screen. I'm going to delete it.
- 1:02:40Yes, I'm going to go to the folder.
- 1:02:45Yes, and I'm going
- 1:02:47to remove it.
- 1:02:52I've deleted the folder.
- 1:02:55Notice how it updates the
- 1:02:58uh folder in the basic Java project; the
- 1:03:00directory folder no longer appears.
- 1:03:03So, let's proceed to run everything
- 1:03:05again.
- 1:03:07If everything is correct, the directory
- 1:03:09called "directorio" should be created,
- 1:03:12and it should save or create two files
- 1:03:14inside that directory.
- 1:03:16Let's run the program. Let me clear this
- 1:03:19little console and let's execute the
- 1:03:22program. Let's see how it goes.
- 1:03:26There it is compiling.
- 1:03:32The directory has been generated. It was
- 1:03:34created correctly.
- 1:03:36I'm going to try opening my folder here
- 1:03:37.
- 1:03:39And there it is. Look at it.
- 1:03:42Inside the directory folder, archivo
- 1:03:4501.txt and archivo 02.txt have been
- 1:03:48created.
- 1:03:52Quite easy.
- 1:03:56Yes, we saw, we already saw how we can,
- 1:03:59uh, create directories with Java.
- 1:04:03Inside a directory, how we can, uh, show
- 1:04:06, uh, sorry, how we can create files
- 1:04:08inside a folder by setting the path.
- 1:04:11Look at the path.
- 1:04:12Perhaps your path when you do it will be
- 1:04:15different from mine.
- 1:04:17Yes, but the important thing is that you
- 1:04:20understand how it works.
- 1:04:23I defined a path, there it is, along
- 1:04:25with the file names, and then I create a
- 1:04:27file-type object called archivo01 and I
- 1:04:30tell it to create, I define an object,
- 1:04:32sorry, I am defining the objects here.
- 1:04:37Let's put it here so you don't get
- 1:04:39confused.
- 1:04:41Uh, here we are going to call them, uh,
- 1:04:44objects.
- 1:04:47Objects, uh, created.
- 1:04:50Or the instances.
- 1:04:55Instances.
- 1:04:57Of objects, for example.
- 1:04:59We created the two instances there; the
- 1:05:01two objects are already created.
- 1:05:03So, these two objects already have the
- 1:05:06paths, uh, inside them. Please mute your
- 1:05:08microphone.
- 1:05:10So, then with the two objects, with
- 1:05:12their paths, uh, set, I tell them to
- 1:05:15create the file here. Create new file.
- 1:05:17Create new file.
- 1:05:27Well, then finally let's see now how, uh
- 1:05:30,
- 1:05:35how we can list the files. That is the
- 1:05:37only thing we are missing. Right there.
- 1:05:40Uh,
- 1:05:44I am going to make, I am going to print
- 1:05:46a small sign.
- 1:05:49To this little sign we are going to put
- 1:05:50, uh,
- 1:05:53of files.
- 1:06:01In the directory.
- 1:06:07All in the directory.
- 1:06:12We can do the same thing I did a moment
- 1:06:14ago.
- 1:06:15Yes, I can go up here. Well, I actually
- 1:06:18deleted it.
- 1:06:20Well, here I can
- 1:06:24print
- 1:06:27a little sign.
- 1:06:30You already know what I am doing.
- 1:06:35Put it here.
- 1:06:38So, here we are going to
- 1:06:42in the directory we are going to put
- 1:06:47directory name directory.
- 1:07:00So, which one is the one that contains
- 1:07:01the name?
- 1:07:02Here, new file.
- 1:07:05There I am going to put new file.
- 1:07:09Name so that it gives me the name.
- 1:07:12Yes, this should work.
- 1:07:15This,
- 1:07:18okay.
- 1:07:24Besides, we already displayed the sign.
- 1:07:27So we are going to define an str
- 1:07:30variable.
- 1:07:32This is going to be an array.
- 1:07:36If you saw the video on arrays, you
- 1:07:37should understand this as well.
- 1:07:39Files, let's name it files here, uh,
- 1:07:42files
- 1:07:46found, for example. Found
- 1:07:51is going to be equal
- 1:07:56to
- 1:08:00what is it going to be equal to. Let's
- 1:08:02see.
- 1:08:13Yes, equal to
- 1:08:18new file dot
- 1:08:22list. That is a method.
- 1:08:25List. If it lets me type it right. List
- 1:08:28.
- 1:08:28There it is.
- 1:08:30That
- 1:08:32method, what it does is it returns, look
- 1:08:35, it returns a string array with the
- 1:08:37files, with the file names.
- 1:08:40That's why this method, what it returns
- 1:08:43, I save it in an array, and since it
- 1:08:46returns a string array, I am storing it
- 1:08:49in a string array called files found.
- 1:08:53So, what is the only thing we have to do
- 1:08:55?
- 1:08:57It's simply, as you already know, with a
- 1:08:59for or for each, we could say, I can say
- 1:09:02int i
- 1:09:06equal to zero with the condition while i
- 1:09:10is less than what?
- 1:09:13Than files found and we invoke a method
- 1:09:18, the length method, length. There it is
- 1:09:22.
- 1:09:24The other is we increment our counter,
- 1:09:28we open a code block, and simply what we
- 1:09:32have to do is print, as easy as that,
- 1:09:36print what
- 1:09:41a file, I can add a label if I want,
- 1:09:44name
- 1:09:47file.
- 1:09:50Yes. And to that I am going to
- 1:09:51concatenate what?
- 1:09:54The iteration I am doing, files found
- 1:09:59and I add the index, and this should
- 1:10:03work.
- 1:10:07Let's see, obviously I am going to have
- 1:10:09to delete everything, I am going to have
- 1:10:12to delete the directory so the process
- 1:10:14runs again.
- 1:10:16So, I'm going to go there, select the
- 1:10:20directory, delete it.
- 1:10:24I already deleted it.
- 1:10:26If you notice, the folder no longer
- 1:10:29appears in my project, it's gone.
- 1:10:33Let's run it. I'm going to clear the
- 1:10:35console.
- 1:10:40Well, that's fine. That will clear when
- 1:10:45when I execute the program.
- 1:10:47So, let's run the program again to see
- 1:10:50what happens.
- 1:10:52It is compiling, cleaning. Let's see.
- 1:10:56And there it is. Look at it.
- 1:10:59If we look at the project, the directory
- 1:11:02folder is created.
- 1:11:04If I open the directory, the two files
- 1:11:08we just created are shown.
- 1:11:12Then, from the program, I list the files
- 1:11:14that the directory contains.
- 1:11:17Look at it there. List of files in the
- 1:11:19directory. Directory name. Look at the
- 1:11:22name there. This comes from a method and
- 1:11:25then with a for loop I display the file
- 1:11:28names. Look, file 01 and file 02.
- 1:11:33With this we complete, uh
- 1:11:37file handling in Java.
- 1:11:43All that remains now is for me to
- 1:11:45provide you with a comprehensive
- 1:11:47practice exercise so you can work
- 1:11:49through all the methods we have covered
- 1:11:52in these last two classes.
- 1:11:55Both videos are available there for you
- 1:11:57to consult again.
- 1:12:00So, I will prepare the practice, and by
- 1:12:03using everything we have learned in this
- 1:12:06class along with the previous video's
- 1:12:09class, you will be able to complete it
- 1:12:12without any issues.
- 1:12:16For your attention,
- 1:12:18thank you very much.
- 1:12:22Yes.
About this transcript
This page contains the full transcript of Java básico 19 - Gestión de archivos en Java - II by JR2B, generated from the public captions YouTube serves with the video. The transcript has 6,698 words across 1,264 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.