I gave a relative path to open a file in the same hierarchy in Java, but it didn't open So when I searched to give an absolute pass, there were few articles, so a memorandum
Is it so easy that no one writes Isn't it written because it is within the scope of common sense? I don't know but it was very easy
Dir.java
import java.io.File;
class Dir {
    public static void main(String[] args) {
        String path = new File(".").getAbsoluteFile().getParent();
        System.out.println(path);
        //Example:C:\Users\xxxxx\Documents\java
    }
}
Another
Dir.java
class Dir {
    public static void main(String[] args) {
        String path = System.getProperty("user.dir");
        System.out.println(path);
        //Example:C:\Users\xxxxx\Documents\java
    }
}
Dir.java
import java.io.File;
public class Dir {
    public static void main(String[] args) {
        String path = new File(".").getAbsolutePath();
        System.out.println(path);
        //Example:C:\Users\xxxxx\Documents\java\.
    }
}
There will be . garbage, but you can replace it ...
I couldn't get it If there is a period in the user name etc., it's all over.
Here is the page that I finally arrived at after searching hard [Java] Get the current directory
Recommended Posts