Solved

directory name problem with new File ( ) command

Posted on 2003-12-10
10
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

630 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