A file object is used to a give a filename. Creating the File object doesn't mean that a file exists. It may be that the does not exist. Suppose if the file exists, first of all we need to convert the file object in URL, for this we use a method toURL(). It returns a URL object and throws MalformedException. After this we will convert this URL to a file object by using getFile() method. We will read this file by using BufferedReader object.
toURL() : It is used to convert the file name into the URL.
getFile() : This is the method of the URL class, is used to get the file name from the URL.
Here is the code of the program:
The output of this program is given below:
The url isfile:/C:/ConstructFileNamePath/ConstructFileNamePath/ConstructFileNamePath.txt
The file name is C:\ConstructFileNamePath\ConstructFileNamePath\ConstructFileNamePath.txt
C:\ConstructFileNamePath\ConstructFileNamePath\ConstructFileNamePath.txt (The system cannot find the path specified)
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial