Converting a Filename to a URL

In this section, you will learn how to converting filename to a url in java.

Converting a Filename to a URL


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:


public class ConstructFileNamePath{
  public static void main(String[] args){
  File file=new File("C:/work/chandan/deepak.txt");
  URL url=null;
  //The file may or may not exist
  url=file.toURL(); //file:/C:/work/chandan/deepak.txt
  System.out.println("The url is" + url);

  // change the URL to a file object
  file=new File(url.getFile());  // c:/work/chandan/deepak.txt
  System.out.println("The file name is " + file);
  int i;
  //opens an input stream
  InputStream is=url.openStream();
  BufferedReader br=new BufferedReader(new InputStreamReader(is));
  }while (i!=-1);
  catch (MalformedURLException e){
  System.out.println("Don't worry,exception has been caught" + e);
  catch (IOException e){

The output of this program is given below:

C:\ConstructFileNamePath>java ConstructFileNamePath
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)

Download this example

Share on Google+Share on Google+

Converting a Filename to a URL

Posted on: June 7, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Converting a Filename to a URL   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 23, 2012
non-determined File URL-path

for my college project I'm trying to connect to a HTML-file within my java project, this however has to be uploaded on our college network to our teacher for review... as such I cannot specify the location through "C:/...../help.html" What is the URL Filepath rule to... say search from within a JAR's root folder? I've tried simply "help.html", "file://help.html". The help.html file IS inside my project folder. I hope this question may be of use to someone else aswell. Thanks in advance
October 6, 2012
The use of file.toURL() has been deprecated

File.toURL(): Deprecated. This method does not automatically escape characters that are illegal in URLs. It is recommended that new code convert an abstract pathname into a URL by first converting it into a URI, via the toURI method, and then converting the URI into a URL via the URI.toURL method.