[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1876
  • Last Modified:

Java.io.File doesn't understand tilde

In unix/linux, you can refer to a user's home directory like this:
~username

instead of
/home/username

I would like to use ~username, sense it is possible that the user's home directory would be at /home/username.

Java.io.File doesn't understand ~username; it only understands /home/username

Is there any solution, or am I stuck having to manually (in code) replace '~' with /home/username or the full path of the location of the user's directory?

Thank you for any help you can give.
0
ctangent
Asked:
ctangent
  • 5
  • 4
  • 2
  • +1
3 Solutions
 
sciuriwareCommented:
You must substitute it by hand.

Remember that JAVA is platform independent and that local jokes
can't be incorporated, otherwise filenames with tilde were impossible.


;JOOP!
0
 
CEHJCommented:
Use

String homeDir - System.getProperty("user.home");
0
 
ctangentAuthor Commented:
CEHJ,

"user.home" won't work because the username I'd be working with might not be the username that the system property returns

sciuriware,
That makes sense.  Thank you for your help.
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!

 
CEHJCommented:
>>"user.home" won't work because the username I'd be working with might not be the username that the system property returns

That would be no different from ~
0
 
ctangentAuthor Commented:
CEHJ,

Ahh.  What I mean is that lets say I have the users bfee and rfoo.

On the unix machines their home dirs are /home/bfee and /home/rfoo.  I can access them in unix by ~bfee and ~rfoo.  But Java.io.File doesn't understand ~bfee, only /home/bfee.

When you say:
String homeDir - System.getProperty("user.home")

Do you mean literarlly
System.getProperty("user.home")
Or could I feasibly do:
System.getProperty("bfee.home")
System.getProperty("rfoo.home")

I have seen the "user.home" property, and am assuming that this just gets the home directory of the current user using the program.  But in this case, the Java.io code is going into a servlet, so the technical user is tomcat.
0
 
objectsCommented:
No you can't use user.home, Java does not provide any support for what you need.

If all the home directorys are located together with standard naming you might be able to get away with

System.getProperty("user.home")+"../"+username
0
 
sciuriwareCommented:
So, ............... you got to code it by hand some way.

;JOOP!
0
 
sciuriwareCommented:
I know of only one solution:

Make a map from /etc/profile.
The key is the first field, the value is the pre-last field: the home directory.

;JOOP!
0
 
ctangentAuthor Commented:
What do you mean by "Make a map from /etc/profile"?

0
 
sciuriwareCommented:
Sorry, my mistake!

I meant : read /etc/passwd.
From those data you can construct a Map object that finds the home directory from any username.

;JOOP!
0
 
ctangentAuthor Commented:
Sciuriware,

Thank you for your help!
0
 
sciuriwareCommented:
:<)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now