Rajendra's Blog
Listing Named Attributes
JSR-203 has a new feature that allows you to access named attributes associated with files. In my previous entry I described how to read and write named attributes in different operating systems. Here is the sample code using JSR-203 APIs lists the named attributes in a directory.
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.*;
public class NamedAttrs {
public static void main(String... args) throws Exception {
final Path path = Paths.get(args[0]);
Files.withDirectory(path, new DirectoryAction() {
public void invoke(DirectoryEntry entry) {
try {
NamedAttributeView nav = entry.getFileAttributeView(NamedAttributeView.class, true);
if (nav == null) {
throw new RuntimeException("Named Attributes are Not Supported");
}
Iterable list = nav.list();
if (list.iterator().hasNext()) {
System.out.println(entry.getName());
for (String item : list) {
System.out.println(" " + item);
}
}
} catch (IOException e) {
System.err.println("problem in reading named Attrs " + e.getMessage());
}
}
});
}
}
This code lists the files associated with named attributes similar to the tools 1. Streams 2. LADS which lists files associated with alternate data streams in Windows NTFS volumes. Windows XP does not have any commands to list the files which have alternate data streams. You can use the above tools to enumerate files which has named attributes. This tools works similar to above code. The greatness of the above code as you know is, it lists named attributes of files in all operating systems but this tools works in only Windows Operating System.
Posted at 03:54PM Jul 29, 2008 by Rajendra Gutupalli in Sun | Comments[0]