[Java] How to get the current directory

Trigger#

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

Easy#

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

Here is it

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
    }
}

bonus#

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.

Thanks

Here is the page that I finally arrived at after searching hard [Java] Get the current directory

Recommended Posts

[Java] How to get the current directory
How to get the date in java
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
[Java] How to get the current date and time and specify the display format
How to get the absolute path of a directory running in Java
[Java] How to get the URL of the transition source
[Java] How to get the maximum value of HashMap
How to use Java HttpClient (Get)
How to get the class name / method name running in Java
[Java] How to use the File class
[Java] How to use the hasNext function
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] How to set the Date time to 00:00:00
[Processing × Java] How to use the class
How to install the legacy version [Java]
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Java8] Search the directory and get the file
[Java] How to convert from String to Path type and get the path
How to get the length of an audio file in java
[Java] How to get and output standard input
How to get today's day of the week
[Java] (for MacOS) How to set the classpath
[Java] How to get random numbers excluding specific numbers
How to use the replace () method (Java Silver)
How to get and study java SE8 Gold
[Java] Memo on how to write the source
How to get the current date as a string in yyyyMMdd format
[Java] How to get to the front of a specific string using the String class
How to get the value after "_" in Windows batch like Java -version
How to create your own annotation in Java and get the value
[Java] How to use Map
[Java] How to omit the private constructor in Lombok
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to write Scala from the perspective of Java
How to use java Optional
How to minimize Java images
How to use java class
[Java] How to use Optional ②
[Android Studio] [Java] How to fix the screen vertically
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to get a request by HTTP communication
[Java] How to easily get the longest character string of ArrayList using stream
[Java] How to use string.format
How to use Java Map
As of April 2018 How to get Java 8 on Mac
How to set Java constants
[Android] How to get the setting language of the terminal
How to use Java variables
[Rails] How to get the contents of strong parameters
How to convert Java radix
[Ruby] How to get the tens place and the ones place
[Java] How to implement multithreading