The Wayback Machine - https://web.archive.org/web/20241126191119/https://www.geeksforgeeks.org/java-program-to-find-current-working-directory/
Open In App

Java Program to Find Current Working Directory

Last Updated : 29 Oct, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The getproperty() methods defined in System Class in Java which is used to retrieve the value of the property named in the argument list. This method property is specified by the key which accepts the parameter. If the property does not exist, this version of getProperty returns null.

Syntax :

public static String getProperty(String key)

Parameter :

  • key: key whose system property we want

Returns :

  • System property as specified the key
  • Null: if there is no property present with that key.

Example:

Java




// Java Program to Find 
// current working directory
import java.io.*;
  
public class GFG {
    
    public static void main(String[] args)
    {
  
        // Getting the path of system property
        String path = System.getProperty("user.dir");
  
        // Printing the path of the working Directory
        System.out.println("Working Directory = " + path);
    }
}


Output:


Previous Article
Next Article

Similar Reads

Java Program to Display Current Hour and Current Minute
The difference between Date and Calendar is that the Date class operates with a specific instant in time and Calendar operates with a difference between two dates. The Calendar class gives you the possibility for converting between a specific instant in time and a set of calendar fields such as HOUR, YEAR, MONTH, DAY_OF_MONTH. Manipulation over the
3 min read
Java program to merge contents of all the files in a directory
Prerequisite : PrintWriter, BufferedReader. We are given a directory/folder in which n number of files are stored(We dont know the number of files) and we want to merge the contents of all the files into a single file lets say output.txt For the below example lets say the folder is stored at the path: F:\GeeksForGeeksFollowing are the steps: Create
2 min read
Java Program to Check if a Directory is Empty or Not
The list() method defined in class java.io.File which is used to obtain the list of files and folders(directories) present in the specified directory defined by its pathname. The list of files is stored in an array of string. If the length of an array is greater than 0, then the specified directory is not empty, else it is empty. Method 1: Use of l
3 min read
Java Program to Search for a File in a Directory
Searching files in Java can be performed using the File class and FilenameFilter interface. The FilenameFilter interface is used to filter files from the list of files. This interface has a method boolean accept(File dir, String name) that is implemented to find the desired files from the list returned by the java.io.File.list() method. This method
3 min read
Java Program to Traverse in a Directory
A directory is an organizational file system structure that contains Files and Directorates. Even an attacker can try to traverse or access a folder which we name as 'File Traversal Attack or Path Traversal Attack a different directory. In short here the directory is traversed which is outside the home/root directory. These files are server-interna
3 min read
Java Program to Get the Size of a Directory
The size of files in Java can be obtained using the File class. The in-built function of 'fileName.length()' is used to find size of file in Bytes. The directory may contain 'N' number of files, for calculating the size of the directory summation of the size of all the files is required. length() Method: length() method in the object of the file cl
2 min read
Java Program to Delete a directory
The class named java.io.File represents a file or directory (path names) in the system. This class provides methods to perform various operations on files/directories. The delete() method of the File class deletes the files and empty directory represented by the current File object. If a directory is not empty or contain files then that cannot be d
2 min read
Java Program to Print the Last Modification Time of a Directory
Last modification date of any Folder/Directories having multiple files in it can be displayed using Java. Using last Modified Time method in Java last modification time of any File in any Folder can be extracted. Approach: In a Folder, we have multiple files in it so there will be multiple dates of formation. Then to get the correct modified date f
3 min read
Unique Windows Directory Program in Java
In the Windows filesystem, two folders/files cannot have the same name. If you enter a folder name that was previously used, the system will have a suffix addition to its name in the form of (k), where, k is the smallest positive integer such that the obtained name remains unique. Problem Statement Given an array of strings dirNames of size n. You
2 min read
Java Program to Create a File in a Specified Directory
Creating a file in a specific directory using Java can be done in various ways. This is done using predefined packages and Classes. In this article, we will learn about how to create a file in a specified directory. Methods to Create a File in a Specified DirectoryThree such methods to create a file in a specified directory are mentioned below: Fil
3 min read
Java Program to List all Files in a Directory and Nested Sub-Directories
Prerequisites: File class Given a main directory/folder, list all the files from it, and if this directory has other nested sub-directories, list files from them. It is pretty easy to observe a simple recursion pattern in the above problem. Algorithm : Create a File object for the main directory.Get an array of files for the main directory.If array
4 min read
Java Program to Display all the Directories in a Directory
The directory is the organizing structure of the computer file system which is not only responsible for storing files but also to locate them on the memory. File organization structure is a hierarchical structure involving parent and child relationships just like what is there in tree data structures. It will vary from operating systems, rather the
3 min read
How to find and open the Hidden files in a Directory using Java
Pre-requisites: Java File Handling So far the operations using Java programs are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file. In this article, we will see how to ope
3 min read
Java Program to Display Current Date and Time
Java is a most powerful programming language, by which we can do many things and Java is an industry preferable language. So it has a huge field of features. Here we discuss one of the best features of Java, that is how to represent the current date and time using Java. There are many ways to do this, there are many classes by which it can be possi
3 min read
Java Program to Add the Data from the Specified Collection in the Current Collection
The grouping of objects in a single unit is called a collection. For example Array, Lists, Hashsets, etc. Data can be added from a specified collection in the current collection by using the addAll() method in Java. This method falls under the header file, java.util.*. The addAll() method returns a true value if the adding of the collection is succ
3 min read
How to Scan All Mp3 Files In the Given Directory using Java?
In this article, we will discuss how to scan all mp3 files and not only from a specific path for an android media player. We will use a recursive method for scanning files. Scan all folders and sort out all mp3 files using the File and FileFilter class. Implementation Java Code import java.io.File; import java.io.FileFilter; // Java program for sca
1 min read
How to Check a File or Directory Exists in Java?
One of the most frequent tasks carried out by a file system in an operating system is checking the existence of a directory or a file. In the form of library functions, most programming languages provide some level of file system accessibility. You will discover how to test an existing file or directory in Java in this post. Checking/Testing the pr
1 min read
Moving a file from one directory to another using Java
Java provides functions to move files between directories. Two ways to achieve this are described here. The first method utilizes Files package for moving while the other method first copies the file to destination and then deletes the original copy from the source. Using Files.Path move() method: Renaming and moving the file permanently to a new l
2 min read
Traversing directory in Java using BFS
Given a directory, print all files and folders present in directory tree rooted with given directory. We can iteratively traverse directory in BFS using below steps. We create an empty queue and we first enqueue given directory path. We run a loop while queue is not empty. We dequeue an item from queue. If the popped item is a directory, get list o
2 min read
How to Create a Directory in Java?
In Java, creating a directory is a common operation when working with file system manipulation. Directories are used to organize files and other directories into a hierarchical structure. This article will guide you through the process of creating a directory in Java, providing step-by-step examples and explanations. Program to Create Current Direc
2 min read
How to List all Files in a Directory in Java?
Java provides a feature to interact with the file system through the java.io.file package. This feature helps developers automate the things to operate with files and directories. In this article, we will learn how to list all the files in a directory in Java. Approaches to list all files in a directory We have two ways to get the list of all the f
3 min read
How to Get the File's Parent Directory in Java?
Java is one of the most popular and powerful programming languages. It is widely used for various purposes. In this article, we are going to learn how to find the parent directory of a file or a specified path. Methods to Get the Parent Directory of a fileIn Java, we can get the parent Directory with the help of two methods. Using java.io.File and
3 min read
How to Watch a Directory for Changes in Java ?
In this article, we will learn how to watch the directory changes through the Java programming language. Java provides a robust and efficient way to monitor directories for changes through the java.nio.file package. The WatchService API allows developers to receive notifications about various events such as file creation, deletion, and modification
2 min read
List all Files from a Directory Recursively in Java
In Java, when working with file systems, sometimes it is necessary to list all files from a directory, including those within its subdirectories. This process, known as recursive directory traversal, allows us to explore the entire hierarchy of files and folders starting from a given directory. Java provides classes like File and Path to handle fil
3 min read
Get name of current method being executed in Java
Getting name of currently executing method is useful for handling exceptions and debugging purposes. Below are different methods to get currently executing method : Using Throwable Stack Trace : Using Throwable Class : In Java, Throwable class is the superclass of all exceptions and errors in java.lang package. Java Throwable class provides several
4 min read
CharacterIterator current() method in Java with Examples
The current() method of java.text.CharacterIterator interface in Java is used to get the current character that is to be read by this CharacterIterator. This method returns that current character. Syntax: public char current() Parameter: This method do not accept any parameter. Return Value: This method returns the current character that is to be r
1 min read
StringCharacterIterator current() method in Java with Examples
The current() method of java.text.StringCharacterIterator class in Java is used to get the current character that is to be read by this StringCharacterIterator. This method returns that current character. Syntax: public char current() Parameter: This method do not accept any parameter. Return Value: This method returns the current character that is
1 min read
BreakIterator current() method in Java with Examples
The current() method of java.text.BreakIterator class is used to get the index of recent text boundary in the line of words. It provides the offset of the text which is currently pointed by the BreakIterator. Syntax: public abstract int current() Parameter: This method does not accept any parameter. Return Value: This method provides the index of r
3 min read
How to Get the Id of a Current Running Thread in Java?
The getId() method of Thread class returns the identifier of the invoked thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused. Java allows concurrent execution of different parts of a progra
4 min read
Naming a thread and fetching name of current thread in Java
Thread can be referred to as a lightweight process. Thread uses fewer resources to create and exist in the process; thread shares process resources. The main thread of Java is the thread that is started when the program starts. now let us discuss the eccentric concept of with what ways we can name a thread. Methods: There are two ways by which we c
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg