Hello friends, In this hub, I will be creating and demonstrating the
working of File objects in Java. Before we go further, lets go in the
basics of importance of files in computers. Generally, data stored in
variables and objects are not permanent. They are not persistent. They
are normally temporary data which remains in memory as long as the scope
of variable. Once a local variable gets out of scope the data is lost. So
to keep the data for long time use generally computer uses files. These
files are been stored in secondary storage devices such as hard disk,
flash drives etc. The data stored in them is permanent.In terms of
programming language such as Java, terms like Files and Streams have
their own meaning. Java actually sees a file as a stream of bytes which
Java provides us a package called as java.io package. This package
consist of class File which helps in retrieving information about various
files and various directories in disk. Generally they don't provide
capabilities such as opening or closing a file and basic processing of a
file or directory. However File objects are frequently used with other
objects in java.io package to do the above processing of the files and
Creating File Objects :-
File Objects are created with new operator, which calls constructor with
four different arguments. One of the constructor requires string passed
to it as argument. This string provides the constructor name of the file
or directory. The name usually is the path given to constructor where
File object can locate the file or directory. The path information
provided into the constructor can be either relative or absolute path.
The relative path deals with current directory from where the application
has began. The absolute path starts from a root directory and proceed to
name of the file or directory specified. Another constructor has two
arguments passed to it, first argument contains the relative or the
absolute path to file directory and the second argument contains the name
of the file or directory , which we want to associate to File object.
Another constructor with File and String argument is used. Here we use
existing File object which points to file or directory specified by the
String argument. The last constructor uses the URI as an argument to
point to the file or directory. Here by URI we mean Uniform Resource
Identifier which are like Uniform Resource Locators. They use URL like
pattern to locate the files, e.g. file://C:/java-programs.txt.
Constructors of File :-
File (String path);
File (String path, String name);
File (File obj, String name);
Important methods of File class
1. boolean isFile() :- This method return either true or false. It
return true if the File object created by passing a String object to it,
is an existing file.
2. boolean isDirectory() :- This method return either true or false.
It return true if the File object created by passing a String object to
it, is an existing directory.
3. boolean exists() :- This method returns true if the file /
directory represented by the object exists.
4. boolean canWrite() :- It returns true if the current application
can write the file.
5. boolean canRead() :- It returns true if the current application can
read the file.
6. boolean isAbsolute() :- It returns true if the path specified as
String in the File object constructor is absolute.
7. String[ ] list() :- If you want directory's content in the form of
array of string this method is used. It returns null if the file
specified in the argument is not directory.
8. long lastModified() :-It provides us with the time, that when a
file was last modified.
9. long length() :- It returns the length of the file in bytes. If its
a directory, an unusual value is returned.
10. String getPath() :- It basically returns the path of the file in
11. String getParent() :- It basically returns the name of parent
directory holding the file .
12. String getName() :- It return name of the file used at the time of
creating File object
13. String getAbsolutePath() :- It basically returns the absolute path
of file or directory.
14. String getCanonicalPath() :- Returns the path of the file object
but separators in the path name are system-dependent separators such as /
15. Boolean isHidden() :- Returns true if the file is hidden and false
16. Boolean renameTo(File name) :- Renames the file (or directory)
represented by this File instance to the new name specified by Name.
17. boolean delete() :- Deletes the file or directory associated with
the File object.
18. boolean createNewFile() :- Creates a new file with the specified
name if it does not exists.
19. boolean mkdir() :- Creates a directory by using the abstract path
name associated with this File instance.
20. boolean mkdirs() :- Creates a directory by using the abstract path
name associated with this File instance. Also creates any non-existent
parent directories appearing in the given path.
21. File [ ] listFiles() :- Returns an array that contains the names of
files only contained in the directory represented by this File instance.