hello
0
Q:

get file path java

 String current = new java.io.File( "." ).getCanonicalPath();
        System.out.println("Current dir:"+current);
 String currentDir = System.getProperty("user.dir");
        System.out.println("Current dir using System:" +currentDir);
2
File file = new File("yourfileName");
String path = file.getAbsolutePath();
1
package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      File f1 = null;
      String v, v1;
      boolean bool = false;
      
      try {
         // create new files
         f = new File("C:\\test.txt");
         f1 = new File("C:\\Program Files");
         
         // get file name or directory name
         v = f.getName();
         v1 = f1.getName();
         
         // true if the file path exists
         bool = f.exists();
         
         // if file exists
         if(bool) {
         
            // prints
            System.out.println("File name: "+v);
         }
         
         // true if the directory exists
         bool = f1.exists();
         
         // if the folder exists
         if(bool) {
         
            // prints
            System.out.print("Folder name: "+v1);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}
0

New to Communities?

Join the community