Solved

directory name problem with new File ( ) command

Posted on 2003-12-10
10
227 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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 …

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now