Forums Register Login

idiom to get the current directory in java7?

+Pie Number of slices to send: Send
I was trying to write a line of code to get the absolute path of the working/current directory. The best I could come up with was this ugly piece of code. How would you make it better? Is there a common idiom?
(1 cow) 1
+Pie Number of slices to send: Send
Hi Jeanne,

Is this what you mean, or is this newbie out of his depth?



cheers,
Daniel
+Pie Number of slices to send: Send
Doesn't System.getProperty("user.dir") do what you want?

I found an old class I wrote two years ago:-That appears to print the absolute path for my current directory.
+Pie Number of slices to send: Send
Why yes, it does. I forgot about that!

Daniel: I gave you a cow for being first with the answer. And for not worrying about pointing out a moderator was missing something obvious!
2
+Pie Number of slices to send: Send
Also:

or
+Pie Number of slices to send: Send
 

Jeanne Boyarsky wrote: . . . Daniel: I gave you a cow for being first with the answer. . . .

And he beat me by two minutes
+Pie Number of slices to send: Send
 

Jeanne Boyarsky wrote:
Daniel: I gave you a cow for being first with the answer. And for not worrying about pointing out a moderator was missing something obvious!



Wow Jeanne, thanks for the cow!
This would be a case of you having forgotten more than I know.

Campbell Ritchie wrote:
And he beat me by two minutes



I would share the cow but it would make a mess of the Saloon
(1 cow) 1
+Pie Number of slices to send: Send
The thing to remember with the user.dir system variable is that it represents the directory from where the JVM was started, which can be anywhere on the filesystem, and it may or may not be what you're after. You definitely can't depend on it to find out the root of the application.
+Pie Number of slices to send: Send
 

Daniel Hirning wrote: . . . I would share the cow but it would make a mess of the Saloon

That doesn't bear thinking about
+Pie Number of slices to send: Send
 

Mike Simmons wrote:Also:

or


Thanks Mike. I didn't realize you could call Paths.get("") with an empty string. That's good to know!
+Pie Number of slices to send: Send
 

Mike Simmons wrote:Also:

or



This is really nice.
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1968 times.
Similar Threads
Using c:if
classpath question
performance degradation in using try/catch
Problem with Java swing directory chooser
Double-checked locking and the Singleton pattern ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:04:31.