0

I want to list NTFS alternate data stream file in a given directory using java. I can do this with the dir /R command on cmd, but I want to do it programmatically.

When I do normal file listing operations, I cannot list alternate data streams in the $Data style.

bloodwork
  • 7
  • 2

2 Answers2

3

This sample code is modeled after Oracles sample code in Managing Metadata (File and File Store Attributes).

It lists the files and directories in the current directory together with the alternate data streams.

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserDefinedFileAttributeView;

public class Xd2 {
    public static void main(String[] args) throws IOException {
        Path p = Paths.get(".");
        Files.list(p).forEach(file -> {
            UserDefinedFileAttributeView view = Files.
                    getFileAttributeView(file, UserDefinedFileAttributeView.class);
            try {
                // print file size and name
                System.out.printf("%8d %s%n", Files.size(file) , file);
                for (String name : view.list()) {
                    // list alternate data stream size and name
                    System.out.format("  %8d  %s\n", view.size(name), name);
                }
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        });
    }
}
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
0

There is no support in Java for this. I am afraid you should do it yourself. It is possible to call a dir /R command and parse output or make a JNI library to call system functions like NtQueryInformationFile.

kan
  • 28,279
  • 7
  • 71
  • 101