![]() The Path, Paths, Files, and many other classes belong to the java.nio package, which was introduced in Java 7 as a more modern way to represent files in a non-blocking way. package ĬurrentDirectory = System.getProperty("user. The Paths class consists of static methods that return a Path based on a String URI - and using a Path, we can locate the file easily. ![]() In this java sample source code we will be illustrating on how to get the current working directory by invoking getProperty() method and passing property name user.dir. Below programs will illustrate the use of the getPath() function: Example 1: We are given a file object of a file, we have to get the path of the file object. Mark's comment is a better solution than lastIndexOf (): file.getParentFile ().getName () These solutions only works if the file has a parent file (e.g., created via one of the file constructors taking a parent File ). Return value: The function returns a String value which is the Path of the given File object. Get Current Directory in java using Property user.dir Use File 's getParentFile () method and String.lastIndexOf () to retrieve just the immediate parent directory. Sample output: Current working directory : C:\Users\ryan\workspace\JavaBasics\. ("Current working directory : " currentDirectory) * using getAbsolutePath() method of File classĬurrentDirectory = file.getAbsolutePath() * how to get the current working directory This sample java source code demonstrates usage of getAbsolutePath() in getting the current directory. Get Current Directory in java using getAbsolutePath() One other solution is get it using the System.getProperty. Basically we will explore getAbsolutePath() method of File class under java.lang package. In this section we will be showing examples on how to get the current directory in java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |