Link to home
Start Free TrialLog in
Avatar of ctangent
ctangent

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
Use

String homeDir - System.getProperty("user.home");
Avatar of ctangent
ctangent

ASKER

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.
>>"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 ~
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, ............... you got to code it by hand some way.

;JOOP!
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!
What do you mean by "Make a map from /etc/profile"?

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sciuriware,

Thank you for your help!