Home Java Example Java Io Constructing a File Name path in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Constructing a File Name path in Java

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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 16, 2007

Related Tutorials

Ask Questions?    Discuss: Constructing a File Name path in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Mritunjay
August 29, 2011
wrong code

the above code does not working properly
Charles
June 27, 2012
This won't work

You will get IOException. Use File.mkdirs to create the path before the file.
savio
November 12, 2012
'error in running the first program above'

Dear Sir, I get the below error when I run the program above in Netbeans editor. Error Details:-- Exception in thread "main" java.io.IOException: The system cannot find the path specified at java.io.WinNTFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(File.java:947) at practicey102.main(practicey102.java:16) Java Result: 1
DMCA.com