1
String currentpath = System.getProperty("user.dir");

System.out.println("current dir = " + currentpath);

It displays my current path- current dir = C:\Users\TKMAHQB\Desktop\New folder\ABC

But I want the path as - C:\Users\TKMAHQB

What shall i do?

Shiv
  • 101
  • 3
  • 15
  • Its not duplicate- I want the path of folder of 3 steps back of user.dir – Shiv Dec 05 '18 at 14:47
  • 1
    Possible duplicate of [How to get just the parent directory name of a specific file](https://stackoverflow.com/questions/8197049/how-to-get-just-the-parent-directory-name-of-a-specific-file) – Joe Dec 06 '18 at 14:04

2 Answers2

3

You may want to use a Path, then call its parents the number of times you need :

System.out.println(Paths.get(currentpath).getParent().getParent().getParent());
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • @Arnaud...Thanks.. it solved my problem..still any other effective way..if any? – Shiv Dec 05 '18 at 14:57
  • @Shiv that is the most reliable way in Java, others via String hacks might be shorter but can be more error prone – UninformedUser Dec 05 '18 at 14:59
  • @Arnaud- when i am changing my folder it crashes, i want to go to that specific Folder only what should i do...like here i want to stop at -MyCode............C:\Users\TKMAHQB\Desktop\MyCode\ABC – Shiv Dec 05 '18 at 15:22
  • There is no issue with the code e.g. my main file is MyCode....1st case) when it is saved at location C:\Users\TKMAHQB\Desktop\MyCode\ABC it works but if i am saving it at C:\Users\MyCode\ABC then it is not working as it will go back to the fixed number of time i have used getParent() – Shiv Dec 05 '18 at 15:29
  • I mean to say i am anywhere in drive or folder i should just go back to MyCode folder.. – Shiv Dec 05 '18 at 15:33
  • I will give a example- 1st case- If i am here, C:\Folder1\Folder2\Folder3\ABC\Folder5\Folder6........2nd case- If i am here, C:\Folder1\Folder2\ABC\Folder3\Folder4.............and i always want to go back to ABC......what will be the common code that will take me to the ABC folder in all the cases wherever i am – Shiv Dec 05 '18 at 15:42
  • I have updated my answer with two possible solutions solving your issue. – rilent Dec 05 '18 at 15:55
  • 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 – Shiv Dec 05 '18 at 15:57
  • `Path` has a `getFileName()` method that will give you the name of the last directory. Just use `getParent()` until you are in the directory with the right name (i.e `"ABC"`). – Arnaud Dec 05 '18 at 16:04
  • Arnaud, can you provide the sample code for the method what you mentioned,pls – Shiv Dec 05 '18 at 18:09
  • Anyways thanks Arnaud, I was able to make the code work using your original post. – Shiv Dec 06 '18 at 05:35
1

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.

rilent
  • 659
  • 1
  • 6
  • 22
  • I want the path of folder of 3 steps back of user.dir – Shiv Dec 05 '18 at 14:44
  • Using "user.home", not "user.dir" should returns C:\Users\TKMAHQB, which is the user's home directory. I believe you didn't read my 2 lines long answer. – rilent Dec 05 '18 at 14:46
  • @rilent- If i want C:\Users\TKMAHQB\Desktop then what will be the code – Shiv Dec 05 '18 at 14:51
  • **String currentpath = System.getProperty("user.home") + File.separator + "Desktop"**; But then if "desktop" can change, you should consider looking at @Arnaud's answer, I believed you wanted the user.home folder, as asked above... – rilent Dec 05 '18 at 14:54
  • Yes got the answer from Arnaud..anyway thanks rilent – Shiv Dec 05 '18 at 14:58
  • @rilent-when i am changing my folder it crashes by Arnaud answer , i want to go to that specific Folder only what should i do...like here i want to stop at -MyCode............C:\Users\TKMAHQB\Desktop\MyCode\ABC – Shiv Dec 05 '18 at 15:24
  • Please provide more code in your question so we can see what you are trying to achieve, I don't understand the problem. – rilent Dec 05 '18 at 15:31
  • There is no issue with the code e.g. my main file is MyCode....1st case) when it is saved at location C:\Users\TKMAHQB\Desktop\MyCode\ABC it works but if i am saving it at C:\Users\MyCode\ABC then it is not working as it will go back to the fixed number of time i have used getParent() – Shiv Dec 05 '18 at 15:32
  • I mean to say i am anywhere in drive or folder i should just go back to MyCode folder.. – Shiv Dec 05 '18 at 15:33
  • If you want to go back to your MyCode folder, then just use the "user.dir" path. Sorry I am not sure I have understood the issue due to the language barrier, post the code so we can see how you use the different paths. – rilent Dec 05 '18 at 15:33
  • The problem appears to be that you fixed the number of times you call **getParent()**, so obviously you are tied to the folder where you are executing the application. This is why system properties are commonly used as It can work on all operating system and so on – rilent Dec 05 '18 at 15:40
  • I will give a example- 1st case- If i am here, C:\Folder1\Folder2\Folder3\ABC\Folder5\Folder6........2nd case- If i am here, C:\Folder1\Folder2\ABC\Folder3\Folder4.............and i always want to go back to ABC......what will be the common code that will take me to the ABC folder in all the cases wherever i am – Shiv Dec 05 '18 at 15:41
  • 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 – Shiv Dec 05 '18 at 15:56
  • 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 : https://www.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 start using getParent() until you reach your ABC folder. – rilent Dec 05 '18 at 15:59
  • Anyways thanks rilent I was able to make the code work using Arnaud original post. Thanks for answering. – Shiv Dec 06 '18 at 05:36