Try this :
String currentpath = System.getProperty("user.home");
Here is a complete Javadoc with all system properties.
user.name -> User's account name
user.home -> User's home directory
user.dir -> User's current working directory
"@rilent- If i want C:\Users\TKMAHQB\Desktop then what will be the code"
String currentpath = System.getProperty("user.home") + File.separator + "Desktop";
If the folder can vary, then look at Arnaud's answer, I thought you needed the user.home folder path.
new edit :
I apologize, i re-frame my question...my last directory is fix....here
is the correct example please ignore above........ case1)
C:\Folder1\Folder2\Folder3\ABC\Folder5\Folder6 i want to navigate to
ABC ....2nd case) C:\Folder1\Folder2\ABC\Folder5\Folder6.....what
should be the way to get to ABC folder from back side
You got two possibilities, if you know the name of the folder, then loop getParent() until you find the wanted folder name, or start from user.home and then build the path to your folder name as shown in my answer above
You expect your application to know whether it has to go back 3 folders or 5 according to its relative position, there is no black magic involved in java... This is why your application crash.
If you want to get it from the back, then you have to search recursively your directory, here is a simple way to do it : mkyong.com/java/search-directories-recursively-for-file-in-java or use File.listFiles(String). I think it is better performance wise to start from the user.dir directory and then do a loop using getParent() until you reach your ABC folder.