• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2053
  • 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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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