0

I have written a grammar that allows the user to input a relative path. (e.g. "../../temp/out/path"

May aim is to get the absolute path based on the input from the user, and the absolute path of the current working directory so that I can also check if the input path is valid or not.

Is there libraries or built in functions that I can use to get the absolute path? Something similar to C's _getcwd() function.

chris yo
  • 1,187
  • 4
  • 13
  • 30

2 Answers2

1

Yes, Java has a File class. You can create one by calling this constructor which takes a String. Then you can call getAbsolutePath() on it. You can call it like this:

package com.sandbox;

import java.io.File;

public class Sandbox {

    public static void main(String[] args) {
        File file = new File("relative path");
        String absolutePathString = file.getAbsolutePath();
    }


}
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
0

This will print a complete absolute path from where your application has initialized.

public class JavaApplication1 {
   public static void main(String[] args) {
       System.out.println("Working Directory = " +System.getProperty("user.dir"));
   }
}
Siddh
  • 712
  • 4
  • 21