Get the directory of the entry file

Discussion in 'Java' started by Allan Bruce, Jun 30, 2004.

  1. Allan Bruce

    Allan Bruce Guest

    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
    Allan Bruce, Jun 30, 2004
    #1
    1. Advertising

  2. 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
    Andrew Thompson, Jun 30, 2004
    #2
    1. Advertising

  3. Allan Bruce

    Chris Smith Guest

    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);
    }

    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
    Chris Smith, Jun 30, 2004
    #3
  4. Allan Bruce

    Roedy Green Guest

    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.
    Roedy Green, Jun 30, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AtomicBob
    Replies:
    14
    Views:
    856
    Toby Inkster
    May 2, 2006
  2. Saqib Ali
    Replies:
    3
    Views:
    912
    Joe Richards [MVP]
    Feb 16, 2007
  3. Mike Owen

    Allowing entry of a Carriage Return during data entry

    Mike Owen, Jul 27, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    679
    Alessandro Zifiglio
    Jul 27, 2006
  4. Kyle Barbour
    Replies:
    10
    Views:
    571
    Marvin Gülker
    Aug 2, 2010
  5. Noozer
    Replies:
    2
    Views:
    237
    Dr John Stockton
    Aug 1, 2005
Loading...

Share This Page