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
 
 
Search All Tutorials
  

 
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
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Constructing a File Name path 

                         

In Java, it is possible to set dynamic path, which is helpful for mapping local  file name with the actual path of the file using the constructing filename path technique.

As you have seen, how a file is created to the current directory where the program is run. Now we will see how the same program constructs a File object from a more complicated file name, using the static constant File.separator or File.separatorCharto specify the file name in a platform-independent way. If we are using Windows platform then the value of this separator is  ' \ ' .

Lets see an example to create a file to the specified location.

import java.io.*;

public class PathFile{
    public static void main(String[] argsthrows IOException{
      File f;
    f=new File("example" + File.separator + "myfile.txt");
      f.createNewFile();
      System.out.println("New file \"myfile.txt\" 

has been created 
                             to the specified location"
);
      System.out.println("The absolute path of the file is: "
                +f.getAbsolutePath());      
    }
}


Output of the program:

C:\nisha>javac PathFile.java

C:\nisha>java PathFile
New file "myfile.txt" has been created to the specified location
The absolute path of the file is: C:\nisha\example\myfile.txt

C:\nisha>

Download this Program

Another program set the dynamic path using File.separator given below:

import java.io.*;

 
public class ConstructingFileNamePath {
 
  
public static void main(String[] args){

      String filepath = File.separatorChar + "tapan"
              
+ File.separatorChar + "joshi";

      System.out.println("The path of the file is  :  " 
              + filepath
);
 
    
  }
 
}


Output of the program:

C:\java>java ConstructingFileNamePath
The path of the file is : \tapan\joshi

Download this example.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (post your own) View All Comments Latest 10 Comments:

What is the difference b/w File.Seperator and File.Seperatorch? Am not able to understand it!!

Posted by Michael Rozar on Tuesday, 01.29.08 @ 16:46pm | #46496

it shuold be in dynamic,means file have to give at the run time and output have to show the file name and path of the file

Posted by sandeep T on Friday, 05.4.07 @ 23:51pm | #15281

i undurstand this code.
but if i want to access value from xml file, how can i code for that in java?

Posted by DHAVAL on Tuesday, 02.27.07 @ 15:16pm | #9830

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.

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

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

Copyright © 2008. All rights reserved.