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

From: Chris Smith (cdsmith_at_twu.net)
Date: 12/29/04


Date: Wed, 29 Dec 2004 13:57:06 -0700

Ramon <ramon@conexus.net> 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[i];
        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


Relevant Pages

  • Re: What is the counterpart of javaxmail api in linux?
    ... > javaxmail api from java on Windows to java on Linux. ...
    (comp.lang.java.developer)
  • Re: Microsoft and Trust Take 2
    ... new API, which *might* be addressable directly via .Net code. ... Either your .Net code is writing to the wrapper ... VB will run on future Windows. ... software with their bloated, heavily dependent, Java clone. ...
    (microsoft.public.vb.general.discussion)
  • Re: OT Java, C#, C++
    ... would you use Java or C++ - or something else? ... locked to 'Windows only'. ... platform' _and_ Windows Only lockin. ... occasionally bypass the API and P/Invoke straight to the Win32 API. ...
    (comp.lang.cobol)
  • Re: any inexpensive C++ API or SDK providers for market trading
    ... > I googled for a C++ API but found only a couple of windows and java ... > What I am really looking for is a C++ API that I can use on my super ... > trading system. ...
    (misc.invest.stocks)
  • Re: AnandTech likes the Mac Pro
    ... Windows in terms of API power, cleanliness, and sheer sanity. ... I agree that ObjC is obsoleted by Java, and that Java is an improvement ... If & when Vista comes out I may install it on my Mac ...
    (comp.sys.mac.advocacy)