Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Get the directory of the entry file

Reply
Thread Tools

Get the directory of the entry file

 
 
Allan Bruce
Guest
Posts: n/a
 
      06-30-2004
Is it possible to get the directory of the file that contains my main()? I
have a few data files in a subdirectory within this directory and I want to
access them like this:

public static final String myDataSubdir = "Data\\";
public static final String myDataFile = "Data1.txt";

// access the file using the below as my String
// myMainDirectory + myDataSubdir + myDataFile


Thanks
Allan


 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      06-30-2004
On Wed, 30 Jun 2004 12:44:21 +0100, Allan Bruce wrote:

> Is it possible to get the directory of the file that contains my main()?


You do not need to.
<http://www.physci.org/codes/javafaq.jsp#path>
Use Class.getResource() instead. E.G.

<sscce>
import java.net.URL;

/** An example to find resources using Class.getResource(). */
public class FindResource {
public static void main(String args[]) throws Exception {
URL resourceURL;
if ( args.length>0 ) {
Object o = new Object();
resourceURL = o.getClass().getResource(args[0]);
System.out.println( "Name/URL: " + resourceName +
" \t/" + resourceURL );
}
}
}
</sscce>
<op>
C:\FindResource>java FindResource /eg/the.bat
Resource - name/URL:/eg/the.bat /file:/C:/FindResource/eg/the.bat

C:\FindResource>java FindResource eg/the.bat
Resource - name/URL:eg/the.bat /null

C:\FindResource>java FindResource the.bat
Resource - name/URL:the.bat /null
</op>

If the above fails, it might require..
C:\FindResource>java -cp . FindResource /eg/the.bat

You might also consider posting to a different
group at least until you become comfortable with
packages and finding (basic) stuff like this..
<http://www.physci.org/codes/javafaq.jsp#cljh>

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      06-30-2004
Allan Bruce wrote:
> Is it possible to get the directory of the file that contains my main()? I
> have a few data files in a subdirectory within this directory and I want to
> access them like this:
>
> public static final String myDataSubdir = "Data\\";
> public static final String myDataFile = "Data1.txt";
>
> // access the file using the below as my String
> // myMainDirectory + myDataSubdir + myDataFile


If you want to read the file, the easiest way is to use a resource:

URL resource = MyMainClass.class.getResource("data/data1.txt");
// Read the URL using openStream

If you need full read/write access, then things get more complicated.
You should first realize that Java is designed to load classes from
anywhere; including read-only resources like HTTP, hard-to-write
locations like a JAR file (which requires rewriting the entire archive
to make general-case modifications), or potentially even auto-generated
code from an intelligent classloader that isn't stored anywhere at all.

Now if you're absolutely certain that you will always be running your
code from class files stored directly in the filesystem (generally an
unnecessarily messy arrangement), then the following code will probably
work for you:

CodeSource source = MyMainClass.class
.getProtectionDomain().getCodeSource();

if (source == null) return null;

File baseDir;

try
{
URI sourceURI = new URI(source.getLocation().toString());
baseDir = new File(sourceURI);
}
catch (URISyntaxException e)
{
return null;
}
catch (IllegalArgumentException e)
{
return null;
}

if (!baseDir.isDirectory()) return null;

String[] classComponents = MyMainClass.class
.getName().split("\\.");

for (int i = 0; i < classComponents.length - 1; i++)
{
baseDir = new File(baseDir, classComponents[i]);
}

File dataDir = new File(baseDir, "data");
File dataFile = new File(dataDir, "data1.txt");

if (!dataFile.isFile()) return null;
else return dataFile;

HTH,

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-30-2004
On Wed, 30 Jun 2004 12:44:21 +0100, "Allan Bruce"
<> wrote or quoted :

>Is it possible to get the directory of the file that contains my main()? I
>have a few data files in a subdirectory within this directory and I want to
>access them like this:


files that live is the same place as you class files are called
"resources". You can get at them with getResource and
getResourceAsStream.

See http://mindprod.com/jgloss/image.html for sample code.

That is the third time I have typed that today. Why is this suddenly a
hot question?

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iteration through File.file? misses entries for whichFile.file?(entry) == true Kyle Barbour Ruby 10 08-02-2010 08:55 PM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Allowing entry of a Carriage Return during data entry Mike Owen ASP .Net Web Controls 3 07-27-2006 02:34 PM
Form field entry directs to diff URLs based on entry? AtomicBob HTML 14 05-02-2006 07:07 AM
Form entry to Time part of database Date entry? Noozer Javascript 2 08-01-2005 08:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57