Solved

directory name problem with new File ( ) command

Posted on 2003-12-10
10
229 Views
Last Modified: 2010-03-31
Hi,

im creating a server. At the moment the user selects the specified directory they which to work from by entering the name of the directory on the command line (via an args request).  

See example below:

           
         directory = new File(args[0]);

What im trying to do is "hard code" the name of the directory into the above example, like:

       directory = new File("somedirectory");

but it won't allow this, can anyone help?????


Ive seen example like :

File directory = new File("C:\\JavaSource");

but as the "class" file could run on any machine, this is not a practical solution

Thanks for any help or advice in advance...
0
Comment
Question by:fimbria
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9911752
File directory = new File("/JavaSource");

will work on any OS (with a "JavaSource" directory at root level)
0
 
LVL 9

Expert Comment

by:Ovi
ID: 9911768
Use system properties.

For example, the bellow line will get you the current working directory:

String currentDir = System.getProperty("user.dir");

The list of all which can be obtained in this way:

java.version Java Runtime Environment version
java.vendor Java Runtime Environment vendor
java.vendor.url Java vendor URL
java.home Java installation directory
java.vm.specification.version Java Virtual Machine specification version
java.vm.specification.vendor Java Virtual Machine specification vendor
java.vm.specification.name Java Virtual Machine specification name
java.vm.version Java Virtual Machine implementation version
java.vm.vendor Java Virtual Machine implementation vendor
java.vm.name Java Virtual Machine implementation name
java.specification.version Java Runtime Environment specification version
java.specification.vendor Java Runtime Environment specification vendor
java.specification.name Java Runtime Environment specification name
java.class.version Java class format version number
java.class.path Java class path
java.library.path List of paths to search when loading libraries
java.io.tmpdir Default temp file path
java.compiler Name of JIT compiler to use
java.ext.dirs Path of extension directory or directories
os.name Operating system name
os.arch Operating system architecture
os.version Operating system version
file.separator File separator ("/" on UNIX)
path.separator Path separator (":" on UNIX)
line.separator Line separator ("\n" on UNIX)
user.name User's account name
user.home User's home directory
user.dir User's current working directory
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9911906
Use the user.home property as anything else will be iffy as:

a. the directory you choose may not exist
b. if it does the user may not have permissions on it
c. you may write something OS-dependent
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 15

Expert Comment

by:jimmack
ID: 9912040
Also, if the directory contains spaces, then the command line argument needs to be contained in inverted commas.

When building a platform independent directory, you need to use the File.separator constant between the directory names, eg:

File dir = new File("dirA" + File.separator + "dirB" + File.separator + "dirC");

Should give

dirA/dirB/dirC on Unix/Linux and
dirA\dirB\dirC on Windows.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9912083
>>Also, if the directory contains spaces...

Of course if you use user.home, this should not be a problem ;-)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9912112
fimbria, could you just clarify this for us please:

>> but it won't allow this,

You mean it doesn't compile (what's the error) or it throws an exception at runtime (what exception) or it simply doesn't contain what you expect?
0
 
LVL 9

Expert Comment

by:Ovi
ID: 9912128
There is just the need to use one of the system properties accesing those directories.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 9912142
>>directory = new File("somedirectory");

If *not* using user.home, you would *always* have to check that this is possible, even if you are sure you have constructed a valid name
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9912163
The question isn't 100% clear, but I get the impression that a "user" will be a client system connecting to the server.  If this is the case, then user.home wouldn't be a viable option, due to the fact that user.home directory would be for the server, not the client.

(Just a thought)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10248446
:-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question