Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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:

import java.io.*;
import java.net.*;

public class ConstructFileNamePath{
  public static void main(String[] args){
    File file=new File("C:/work/chandan/deepak.txt");
    URL url=null;
    try{
      //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));
      do{
        i=br.read();
        System.out.println((char)i);
      }while (i!=-1);
      is.close();
    }
    catch (MalformedURLException e){
      System.out.println("Don't worry,exception has been caught" + e);
    }
    catch (IOException e){
      System.out.println(e.getMessage());
    }  
  }
}

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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
add picture to a swing
diplay North American
how to create war file
setBounds
Excel open java applic
jsp:plugin attribute
how to change url
selecting option value
dynamic array value
Javascript Mouse Trick
struts2 action element
jsp bean
struts 2 ajax
return type-methods
actionperformed
a simple calculator pr
relative path in web a
visual C 6.0
html sample
insert image in databa
how to get the time di
How to get data from d
code for how i reduce
sql timestamp examples
update query Example
polemophsm
csv files database
submit button in Frame
java strings
JFreechart Library: Hi
string to date
how to hide a select t
to find the max of thr
Inserting values in My
JSTL : Submit Form Tex
file upload in structs
Messenger in java
jsp bean get property
login window
program in java to imp
sum of integer
servlet & jsp
ResourceBundle
sum of rows and coloum
JCAPS
Qt Jambi
logout ajax struts2
calculate mode in Java
input in java
merge sort
jms examples
HTML form to upload fi
coding of button in se
A�������
activate email account
string to integer java
tree and treenode (Aja
Photoshop Animation Ba
Java code for media pl
PrintWriter(System.out
increase size of strut
java programm for arra
adding labels to a fra
emailing file in java
how to get a values fr
how to print arraylist
multiple submit button
how to compare two tim
What are the defined J
code for Login Applica
<c:forEach
applet message frame
finding superscript in
error-Unknown entity:
<c:if test
Easy UML Diagrams
Jenia
netui:select
Fireworks Text Effects
h:inputtext tag in jsf
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.