0

When I execute the following code:

File f = getFile(); String absPath = f.getAbsolutePath() System.out.println(absPath); 

I get the following print out:

C:\Users\myUser\Desktop\test\file.txt

But that's not what I want! I want the absolute path leading up to file.txt, i.e.:

C:\Users\myUser\Desktop\test\

I'm trying to figure out how to use the Java File I/O API to accomplish this but I can't seem to find anything that is a match for what I'm looking for. Thanks for any nudges in the right direction.

2 Answers 2

3

Look here: File#getParentFile()

Sign up to request clarification or add additional context in comments.

5 Comments

This was the first right answer! I saw your deleted comment!! haha! ;)
I would, if google wouldn't bring it to the first search hit, everytime I search for java-docs.
type "6" (or even "7") after the class. The top result is for 1.4.2 because people still give links to it.
I changed th link. By the way I think 7 is still to new :-)
"I think 7 is still to new " The v. 7 docs has everything the 6 docs have, as well as @since and @deprecated flags. The first warns you of new functionality that might not be in your target JRE. The latter warns of methods that you are using at the moment, which might be about to be deprecated come the next version. As such, it would pay to start changing the code right now, in anticipation. In short, there is no good reason for linking to anything but the latest docs that are available (at Oracle, not the dev. site).
2

The getParentFile() method of File is your friend here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.