Re: Looking for a class similar to 'opendir' and 'readdir'
From: Chris Smith (cdsmith_at_twu.net)
Date: 12/29/04
- Next message: ByteCoder: "Re: decompress Huffmann with java.util.zip?"
- Previous message: Andrey Kuznetsov: "Re: decompress Huffmann with java.util.zip?"
- In reply to: Ramon: "Looking for a class similar to 'opendir' and 'readdir'"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: ByteCoder: "Re: decompress Huffmann with java.util.zip?"
- Previous message: Andrey Kuznetsov: "Re: decompress Huffmann with java.util.zip?"
- In reply to: Ramon: "Looking for a class similar to 'opendir' and 'readdir'"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|