1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* 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 dir1dir2 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 : JavaExamplesIO Output in UNIX machine would be, File path is : /JavaExamples/IO */ |
Add Comment