Java Program to Get Current Working Directory

Java Program to Get Current Working Directory

What are Directories?

In Java, a directory is a file path that points to a location on a file system where multiple files and other directories can be stored. It is used to organize and group files and folders in a hierarchical structure. A directory can be created, deleted, and navigated using the java.io.File class, which provides methods for working with directories in a platform-independent way.

In this Article, we will write a program to Delete Empty and Non-empty Directory.

Directories in Java :

In Java, directories are represented by the java.io.File class, which provides methods for working with directories in a platform-independent way. The File class can be used to create, delete, and navigate directories, as well as perform other operations such as checking if a file or directory exists, or getting information about a file or directory.

Some of the commonly used methods of the File class for working with directories include:
  • File.mkdir(): creates a new directory
  • File.mkdirs(): creates a new directory, including any necessary but nonexistent parent directories
  • File.list(): returns an array of strings naming the files and directories in the directory
  • File.listFiles(): returns an array of File objects for the files and directories in the directory
  • File.isDirectory(): returns true if the file is a directory, false otherwise
  • File.delete(): deletes the directory and all the files and subdirectories inside it

It is important to note that the File class only provides methods for working with directories on the local file system. To work with directories on a remote file system or network, such as a network file share or FTP server, you will need to use a different library or framework.

Program to Get current working directory :

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        
        // Get the current working directory
        String currentWorkingDirectory = System.getProperty("user.dir");

        // Print the current working directory
        System.out.println("Current working directory: " + currentWorkingDirectory);
    }
}

Explanation:

In this example, the program uses the System.getProperty(“user.dir”) method to get the current working directory. The user.dir property returns the current working directory of the user’s system. The program then prints the current working directory using the System.out.println() method.

Program to Delete a non-empty directory :

Run
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main{
    public static void main(String[] args){
        
        // Get the current working directory
        Path currentWorkingDirectory = Paths.get("").toAbsolutePath();

        // Print the current working directory
        System.out.println("Current working directory: " + currentWorkingDirectory);
    }
}

Explanation:

In this example, the program uses the Paths.get(“”).toAbsolutePath() method to get the current working directory. The Paths.get(“”) method returns a Path object representing the current working directory. The toAbsolutePath() method returns the absolute path of the Path object. The program then prints the current working directory using the System.out.println() method.

You can also use Paths.get(“.”).toAbsolutePath() to get the current working directory.

It is important to note that, Paths.get() returns a Path object that may not exist, and toAbsolutePath() will return the normalized, absolute path of the path, resolving any symbolic links and references to “.” and “..” in the path.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription