/*
Construct file path
This Java example shows how to construct a file path in Java using File class.
*/
import java.io.*;
public class ConstructFilePath {
public static void main(String[] args) {
/*
* To create a file path use File.separator constant defined in
* File class.
*
* Windows based machines has path like \dir1\dir2 while UNIX based machines
* has path like /dir1/dir2
*/
String filePath = File.separator + "JavaExamples" + File.separator + "IO";
//create new File object
File file = new File(filePath);
/*
* Please note that creating file object DOES NOT actually create a file.
* It DOES NOT have any effects on the filesystem.
*/
//display file path using getPath() method of File class.
System.out.println("File path is : " + file.getPath());
}
}
/*
Output in Windows machine would be,
File path is : \JavaExamples\IO
Output in UNIX machine would be,
File path is : /JavaExamples/IO
*/
Bookmark/Search this post with:
Post new comment