DirectoryStream on Windows
Matthias Bergk
matthias.bergk at crossmediasolutions.de
Wed Nov 23 06:53:44 PST 2011
Dear all,
I examined the file attributes that come with a directory stream on Windows.
I found that the returned Path objects are of the class WindowsPath$WindowsPathWithAttributes.
But how to get at those attributes? The following code is taken from JDK sample code:
try( DirectoryStream<Path> stream = Files.newDirectoryStream(folder) )
{
for( Path entry : stream )
{
BasicFileAttributeView v =
Files.getFileAttributeView(entry,BasicFileAttributeView.class);
BasicFileAttributes a = v.readAttributes();
System.out.println( entry+" "+a.size() );
}
}
Unfortunately this code does not benefit from the file attributes that are available in the entries.
Rather, it reads them anew from the system.
Using "reflection" it is possible to get at the attributes contained in the entries:
try( DirectoryStream<Path> stream = Files.newDirectoryStream(folder) )
{
for( Path entry : stream )
{
Field fRef = entry.getClass().getDeclaredField("ref");
if( !fRef.isAccessible() )
fRef.setAccessible(true);
WeakReference<BasicFileAttributes> ra =
(WeakReference<BasicFileAttributes>) fRef.get(entry);
BasicFileAttributes a = ra.get();
System.out.println( entry+" "+a.size() );
}
}
This code is considerably faster if the folder is on a network and contains many files.
Of course, I would prefer to read the attributes without reflection in the intended way.
Best regards,
Matthias Bergk
Software-Entwicklung
CMS - Cross Media Solutions GmbH
Beethovenstraße 5 A
97080 Würzburg
Tel: +49 (931) 385 - 369
Fax: +49 (931) 385 - 364
E-Mail: matthias.bergk at crossmediasolutions.de
Internet: http://www.crossmediasolutions.de <http://www.crossmediasolutions.de/>
Handelsregister: Würzburg HRB 8950
Geschäftsführer: Michael Schardt
member of Euradius
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20111123/9470a74c/attachment-0001.html
More information about the nio-dev
mailing list