Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Constructing a File Name path in Java 
 

In this section, you will learn about constructing a file name path through the java program.

 

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.

                         

» View all related tutorials
Related Tags: c orm form time script object io objects help method sed system ip collection this opera create show for work

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 

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

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

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 | Flex 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.