Home > Net >  Testing for local or remote path (without sun.awt.shell.ShellFolder class)
Testing for local or remote path (without sun.awt.shell.ShellFolder class)

Time:03-29

I am trying to move from Java 15 to Java 17 but because they now enscapulate the internals and have removed the flag –illegal-access I have a problem.

From enter image description here

CodePudding user response:

Note that ShellFolder is the backend of the FileSystemView API which provides most of the functionality to present file information in a UI. Testing whether a file is on a network drive isn’t provided though, but your original code looks more like a heuristic anyway.

The simplest test I’ve found so far, based on NIO only, is for the Volume Serial Number, which is only available for local drives:

public class WindowsFilesystemType {
    public static boolean isNTFSOrFAT32(String newPath) {
        try {
            return switch(Files.getFileStore(Paths.get(newPath)).type()) {
                case "NTFS", "FAT", "FAT32", "EXFAT" -> true;
                default -> false;
            };
        } catch (IOException ex) {
            MainWindow.logger.log(Level.SEVERE, ex.getMessage(), ex);
            return false;
        }
    }

    public static boolean isRemote(String newPath) {
        try {
            FileStore fileStore = Files.getFileStore(Paths.get(newPath));
            return Integer.valueOf(0).equals(fileStore.getAttribute("volume:vsn"));
        } catch(Exception ex) {
            MainWindow.logger.log(Level.SEVERE, ex.getMessage(), ex);
            return false;
        }
    }

    public static boolean isLocal(String newPath) {
        try {
            FileStore fileStore = Files.getFileStore(Paths.get(newPath));
            return !Integer.valueOf(0).equals(fileStore.getAttribute("volume:vsn"));
        } catch(Exception ex) {
            MainWindow.logger.log(Level.SEVERE, ex.getMessage(), ex);
            return false;
        }
    }
}

This Windows file store attribute has not been documented, but it works since Java 7.

  • Related