Looking for a class similar to 'opendir' and 'readdir'

Discussion in 'Java' started by Ramon, Dec 29, 2004.

  1. Ramon

    Ramon Guest

    Hello:

    I need to write a simple utility which will navigate
    several subdirectories and query the file size of
    some files.

    I am very familiar with the Unix 'opendir' and 'readdir'
    functions, and that's what I am looking for, except
    that this time I need something for Java, and the
    utility will be run on a Windows PC.

    Also: has Java been able to come up with a working
    'chdir()' (on Windows, that is)? Last time I checked
    the C++ chdir function in Windows exists and succeeds
    but it is bogus, since doesn't really change the program's
    default directory.

    TIA,

    -Ramon F Herrera
     
    Ramon, Dec 29, 2004
    #1
    1. Advertising

  2. Ramon

    Ann Guest

    "Ramon" <> wrote in message
    news:...
    > Hello:
    >
    > I need to write a simple utility which will navigate
    > several subdirectories and query the file size of
    > some files.
    >
    > I am very familiar with the Unix 'opendir' and 'readdir'
    > functions, and that's what I am looking for, except
    > that this time I need something for Java, and the
    > utility will be run on a Windows PC.


    File dir = new File(".");
    String files[] = dir.list();

    > Also: has Java been able to come up with a working
    > 'chdir()' (on Windows, that is)? Last time I checked
    > the C++ chdir function in Windows exists and succeeds
    > but it is bogus, since doesn't really change the program's
    > default directory.


    You don't need this, why do you think you want it?

    >
    > TIA,
    >
    > -Ramon F Herrera
    >
     
    Ann, Dec 29, 2004
    #2
    1. Advertising

  3. Ramon

    Guest

    I would suggest looking at the java.io.File class. There are methods to
    obtain directory listings and get file sizes.
     
    , Dec 29, 2004
    #3
  4. Ramon

    Ramon Guest

    Hi Ann:

    Thanks for your answer. It's helpful and makes sense, too.

    > You don't need this, why do you think you want it?


    I'll answer the above in a general manner.
    I can think of 2 reasons why I like to be able to use relative
    paths and change default directory at run time:

    (1) I like a world were I have options and choices.
    (2) I don't like being lied to.
    A 'chdir()' call that does nothing is a lie.

    -Ramon
     
    Ramon, Dec 29, 2004
    #4
  5. Ramon

    Chris Smith Guest

    Ramon <> wrote:
    > I need to write a simple utility which will navigate
    > several subdirectories and query the file size of
    > some files.
    >
    > I am very familiar with the Unix 'opendir' and 'readdir'
    > functions, and that's what I am looking for, except
    > that this time I need something for Java, and the
    > utility will be run on a Windows PC.


    See java.io.File and its methods. Specifically, if you have a known
    starting point as a path string, create a File object to represent it, a
    la:

    File myDir = new File(path);

    Then use listFiles to get the contents of the directory:

    File[] contents = myDir.listFiles();

    Then use the various methods of java.io.File (see the API docs for
    details) to get the attributes of the file:

    for (int i = 0; i < contents.length; i++)
    {
    File f = contents;
    System.out.println(f.getName() + ": " + f.length());
    }

    (or, in the new Java 1.5)

    for (File f : contents)
    {
    System.out.println(f.getName() + ": " + f.length());
    }

    > Also: has Java been able to come up with a working
    > 'chdir()' (on Windows, that is)? Last time I checked
    > the C++ chdir function in Windows exists and succeeds
    > but it is bogus, since doesn't really change the program's
    > default directory.


    Java has never provided an API for changing the current working
    directory. However, as of Java 1.3, you can specify the working
    directory for any subprocesses created via Runtime.exec calls.

    I don't know what your complaint is with changing the current working
    directory on Windows from C++. There is no 'chdir' function on Windows,
    since chdir is specified by POSIX and not by ANSI C or C++. There is,
    however, a Win32 API function called SetCurrentDirectory. There are
    also functions called _chdir and _wchdir in the Microsoft Visual C++
    runtime library, which are fairly similar to POSIX chdir. These all
    seem to work, but it may lead to undefined results to use them in native
    code from a multithreaded (meaning pretty much any) Java application.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Dec 29, 2004
    #5
  6. Ramon wrote:

    > Hi Ann:
    >
    > Thanks for your answer. It's helpful and makes sense, too.
    >


    [About a chdir() feature:]

    >>You don't need this, why do you think you want it?

    >
    >
    > I'll answer the above in a general manner.
    > I can think of 2 reasons why I like to be able to use relative
    > paths and change default directory at run time:
    >
    > (1) I like a world were I have options and choices.


    Changing the default directory is an inherently unsafe thing for a
    multithreaded program to do, and Java is designed to be relatively easy
    for multithreaded programming. If you consider that JVMs are generally
    multithreaded, then Java programs are essentially *all* multithreaded
    programs. At the very least they are potentially multithreaded.

    Wanting options and choices is fine in general, but that doesn't mean
    anything about whether specific options or choices ought to be available.

    As for relative paths, you can use them very easily in Java. Look some
    more at java.io.File. The difference with Java is that if you want to
    resolve a relative path against a directory different from the
    application's default then you provide the base path. As an aside,
    "File" is a rather unfortunate choice of name for the system class in
    question, because the class has little to do with actual files. It is a
    class representing paths in a hierarchical filesystem.

    > (2) I don't like being lied to.
    > A 'chdir()' call that does nothing is a lie.


    Not relevant to Java.


    John Bollinger
     
    John C. Bollinger, Jan 3, 2005
    #6
    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. electric sheep

    readdir() and S_ISDIR(stat.st_mode) question (OT ?)

    electric sheep, Mar 4, 2004, in forum: C Programming
    Replies:
    2
    Views:
    11,579
    Villy Kruse
    Mar 4, 2004
  2. John Stoffel
    Replies:
    0
    Views:
    164
    John Stoffel
    Feb 6, 2009
  3. it_says_BALLS_on_your forehead

    opendir and readdir vs glob iteration

    it_says_BALLS_on_your forehead, Dec 1, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    479
    robic0
    Dec 3, 2005
  4. Davy
    Replies:
    2
    Views:
    225
  5. poncenby

    readdir and regex

    poncenby, Jan 24, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    254
    -berlin.de
    Jan 24, 2007
Loading...

Share This Page