Re: How to find the startup directory of my program?
- From: "Ike" <rxv@xxxxxxxxxxx>
- Date: Sat, 26 Nov 2005 21:45:45 GMT
If you;re talking about windows, here is what I use:
.....= (new LocalDir()).getLocalDirRef();
//Ike
/*
* localDir
*
* Utility class to get String and File reference to the local directory
* that the class is executing in.
*/
import java.io.File;
/**
*
* @author Mark Kozel
* @version 1.4.1
* @since Created on December 16, 2002
*/
public class LocalDir
{
public LocalDir()
{
}
/**
* Returns the disk file name of the class that is executing.
*
* @param none
* @return Name of class that is currently executing
*/
public String getClassName()
{
String thisClassName;
//Build a string with executing class's name
thisClassName = this.getClass().getName();
thisClassName = thisClassName.substring(thisClassName.lastIndexOf(".")
+ 1,thisClassName.length());
thisClassName += ".class"; //this is the name of the bytecode file
that is executing
return thisClassName;
}
/**
* Returns the name of the local directory based on the results of a call
to getClassName()
*
* @param none
* @return Name of directory that contains the executing class
*/
public String getLocalDirName()
{
String localDirName;
//Use that name to get a URL to the directory we are executing in
java.net.URL myURL = this.getClass().getResource(getClassName());
//Open a URL to the our .class file
//Clean up the URL and make a String with absolute path name
localDirName = myURL.getPath(); //Strip path to URL object out
localDirName = myURL.getPath().replaceAll("%20", " "); //change %20
chars to spaces
//Get the current execution directory
localDirName =
localDirName.substring(0,localDirName.lastIndexOf("/")); //clean off the
file name
return localDirName;
}
/**
* Returns a File reference to the local directory based on the results
of a call to getClassName()
*
* @param none
* @return File object that points to the local directory
*/
public java.io.File getLocalDirRef()
{
File myFileObj;
//Make the file object and return it
myFileObj = new File(getLocalDirName());
return myFileObj;
}
}
.
- References:
- How to find the startup directory of my program?
- From: Joost Kraaijeveld
- How to find the startup directory of my program?
- Prev by Date: Re: [Newbie Question] Magic Number's dangerous?
- Next by Date: Possible to test if file is open in another process?
- Previous by thread: Re: How to find the startup directory of my program?
- Next by thread: Context.lookup
- Index(es):
Relevant Pages
|