• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get unix machine name and find if a specified file exist ?  RSS feed

 
mike zhang
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to do 2 things: find out the unix machine name, and finds if a specified file exists in a certain directory.
I am familiar with unix script so I know it is very easy to do it using script (use 'uname'..). Can somebody tell me what's the java syntax to do these two things on unix (assume my code is on UNIX only) ? Plus, in java, is it fast to find if a specified file exist in a certain directory assuming there may be 1000 files over there ?
thanks,
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) String localHostname = java.net.InetAddress.getLocalHost().getHostName();
2) boolean exists = new File("myDirPath"+File.pathSeperator+"theFileName").exists();
 
Steve Yu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Ross:
1) String localHostname = java.net.InetAddress.getLocalHost().getHostName();
2) boolean exists = new File("myDirPath"+File.pathSeperator+"theFileName").exists();

Thanks, but java.net.InetAddress.getLocalHost().getHostName();
returns an abbraviation type name, for example, it returns something like "kudo" instead of "kudo.mycom.org" kind full name. Is there any method returns a full host name ?
 
norman richards
Author
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are on 1.4, try getCanonicalHostName() instead. If you don't get the domain added on, it's most likely because your machine isn't configured with a domain.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!