Convert a file name path to url and vice versa in java

In this section, you will learn about the conversion from a Filename path to a URL and vice versa in the java.

Ads

String representation of the URL: 

This program also converts the Filename Path into URL. It reads a file name and converted into the URL.

toURL():

The toURL() method of the File class is used to convert the file name path into the URL. This is done by creating the instance of the URL class with the toURL() method of the File class.

Here is the code of Convert the Filename Path into URL Program :

import java.io.*;

  import java.net.*;

  public class ConvertFileNamePathToURL{
 
  
public static void main(String[] args)throws IOException{

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Please enter the File neme : ");
 
 
String str = in.readLine();
 
  
File file = new File(str);
 
  
URL url = null;
 
 
try{
 
 
url = file.toURL();
 
  
System.out.println("URL : "+ url);
 
  
System.out.println("Converting process Successfully");
 
 
}
  
  
catch (MalformedURLException me){
 
  
System.out.println("Converting process error");
  
  
}
 
 
}
 
}

Download this example.

Advertisements

Ads
Share on Google+Share on Google+

Convert a file name path to url and vice versa in java

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

Advertisements

 

Discuss: Convert a file name path to url and vice versa in java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads