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

R

Ramon

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
 
A

Ann

Ramon said:
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?
 
K

klynn47

I would suggest looking at the java.io.File class. There are methods to
obtain directory listings and get file sizes.
 
R

Ramon

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
 
C

Chris Smith

Ramon said:
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
 
J

John C. Bollinger

Ramon said:
Hi Ann:

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

[About a chdir() feature:]
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
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top