[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Retrieve parent directory in java

Posted on 2011-10-18
6
Medium Priority
?
240 Views
Last Modified: 2012-06-21
Please let me know how I can retrieve the parent directory in java.

If possible, please provide some sample code. Thanks!
0
Comment
Question by:dshrenik
  • 3
  • 3
6 Comments
 

Author Comment

by:dshrenik
ID: 36990855
It will be great if this works across all OSs.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36990858
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36990861
If you want then to get name - you then cal

 f.getParent().getName();
or
f.getParent().getAbsolutePath()

It should work for all OS
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dshrenik
ID: 36990865
That doesn't work.
Though I replaced File with String.
0
 

Author Comment

by:dshrenik
ID: 36990868
This works:
            String currentDir;
            currentDir = System.getProperty("user.dir");
            System.out.println(currentDir);
            
            File f= new File(currentDir);

            String parentDir = f.getParent();
            System.out.println(parentDir);
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36990881
yes, getParentFile() returns File and getParent() returns String
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question