Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Looking for a class similar to 'opendir' and 'readdir'

Reply
Thread Tools

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

 
 
Ramon
Guest
Posts: n/a
 
      12-29-2004
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

 
Reply With Quote
 
 
 
 
Ann
Guest
Posts: n/a
 
      12-29-2004

"Ramon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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
>



 
Reply With Quote
 
 
 
 
klynn47@comcast.net
Guest
Posts: n/a
 
      12-29-2004
I would suggest looking at the java.io.File class. There are methods to
obtain directory listings and get file sizes.

 
Reply With Quote
 
Ramon
Guest
Posts: n/a
 
      12-29-2004
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

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      12-29-2004
Ramon <(E-Mail Removed)> 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
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      01-03-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
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
Any similar Webcam broadcasting site similar to youtube Chaudhry Nijjhar Computer Support 0 02-19-2008 11:48 PM
Looking for something similar to Cisco 3000 series Ken Cisco 4 02-08-2007 07:11 AM
Looking for a control similar to mobile ObjectList control JJ ASP .Net 0 10-27-2006 06:54 PM
Deep Freeze; looking for something similar socalmk@gmail.com Computer Security 4 02-17-2006 06:13 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments