Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

directory name problem with new File ( ) command

Posted on 2003-12-10
10
Medium Priority
?
234 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

773 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