In my last post I explained how to create files.
Today i
will resume from there.
Creating folders also can be done in a similar manner.
File fol1=new File("firstfolder");
The name of the folder that you want to create (firstfolder)
should be passed into the constructor of the File object "fol1" that we are
creating. Actually this will create only the name of the folder, to create the
actual folder we have to call the mkdir() method in our fol1 object.
fol1.mkdir();
This will create a folder called "firstfolder" in
the location where you save this java file.
If you want to create it in a different
location you can give that location as follows.
File fol1=new
File("E:/image/firstfolder");
Please note that "/" character should be used when
specifying a path in JAVA.
After compiling and running the program as follows
You will able to see the folder which you have created...:)
Ok...now let's see how can we create a file inside a folder.
Let's create a file called "firstfile.exe" inside the folder which we
have created.
In
this code I have only added two more lines...:)
File
file1=new File(fol1,"firstfile.txt");
When
creating a file inside a folder, as I mentioned in my first post we have to
first create a File object. The only difference here is we have to pass two
parameters in to the constructor of the
object that we are creating.
- First we have to mention the File object ("fol1") corresponding to the folder which we are going to create our file in. You should create this folder before creating the file.
- Secondly pass the name and the file type of the file that we wish to create ("firstfile.txt").
Even
you can use an excising folder for this. If you wish to do so, first you must
create a File object refering that folder.
ex:
File
myown=new File("E:/image/myfolder");
then
you can proceed as usual, in this case calling mkdir()
is not needed since we are not creating a new folder.
File
file1=new File(myown,"firstfile.txt");
file1.createNewFile();
After compling and running, you will able to see ur new
file created inside the folder which you specified.. :)
Ok...........
now let's see how to detete a file.
In deleting also, the first thing we have to do is
creating a File object which refers to the file which we want to delete.
Let's delete this "Desert.jpg" file in this
location
Here is the code written for that
After creating the File object "Des" which refers
to the file which we wish to delete, the delete() method
should be called.
Des.delete();
After compiling and running you will notice that the "Desert.jpg"
is no more there.....:)
Please note that by this method the file will be completely
deleted WITHOUT moving to the Recycle Bin..!!
Folders can also be deleted in a similar manner..... But you
will only able to delete folders which are EMPTY. You won't able to delete a
folder if it contains any files...!! You
must first delete the files inside it before deleting such folder...:)
Let's delete this "Foldername" folder which is
empty.
After compiling and running the following code, you will
note that it is no more....
That's all for this post............:)
Hope you have understood my explanation..
If you have any doubts on this post please feel free to
ask........I will answer them as much as possible..)
See You soon with another post.....
Bye for now...!
TC....:)