Link to home
Start Free TrialLog in
Avatar of fimbria
fimbria

asked on

directory name problem with new File ( ) command

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...
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

File directory = new File("/JavaSource");

will work on any OS (with a "JavaSource" directory at root level)
Avatar of Ovi
Ovi

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
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
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.
>>Also, if the directory contains spaces...

Of course if you use user.home, this should not be a problem ;-)
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?
There is just the need to use one of the system properties accesing those directories.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
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)
:-)