3

This is required for Windows machines only to decide whether to enforce Windows Explorer 259 path char limit. I have the following method that uses Filestore determine if partition is a windows based partition but I also need to know if local or remote drive.

Im using Java 8.

Update

I tried against root of networked drive mounted on Z:\, this actually throws an exception so that does help deterine if networked or not, but I cant see how to know that Z:\Drive is mounting \nas because if I want to find fs I need to use \nas ?

        public static boolean isNTFSOrFAT32(String newPath)
            {
                Path root = Paths.get(newPath).getRoot();
                if(root==null)
                {
                    return false;
                }
                try
                {
                    FileStore fs = Files.getFileStore(root);
                    if (fs.type().equals("NTFS") 
                    || fs.type().equals("FAT")
                    || fs.type().equals("FAT32))
                    {
                        return true;
                    }
                    return false;
                }
                catch(IOException ex)
                {
                    MainWindow.logger.log(Level.SEVERE, ex.getMessage(), ex);
                    return false;
                }
            }

I would prefer a proper pure Java implementation, the answer given as a possible duplicate Java: how to determine the type of drive a file is located on? is rather flaky code

Update

package com.test;
import java.nio.file.FileStore;
        import java.nio.file.Files;
        import java.nio.file.Path;
        import java.nio.file.Paths;
        import java.util.Iterator;

public class Fs
{
    public static void main(String[] args) throws Exception
    {
        Path p = Paths.get("Z:\\\\").getRoot();
        System.out.println(p+":"+Files.exists(p));
        Iterator<FileStore> i = p.getFileSystem().getFileStores().iterator();
        while(i.hasNext())
        {
            FileStore fs = i.next();
            System.out.println("--" + fs.type() + ":" + fs.name() + ":" + fs.getTotalSpace());
        }
        //Files.getFileStore(p);

        Path p2 = Paths.get("\\nas").getRoot();
        System.out.println(p2+":"+Files.exists(p2));
        FileStore fs2 = Files.getFileStore(p2);
        System.out.println("--" + fs2.type() + ":" + fs2.name() + ":" + fs2.getTotalSpace());
        FileStore fs = Files.getFileStore(p);
    }
}

Outputs

Z:\:false
--NTFS::478854123520
--NTFS:New Volume:1000202039296
--exFAT:MacMusic:2000378920960
\:true
--NTFS::478854123520
Exception in thread "main" java.nio.file.NoSuchFileException: Z:\
    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsLinkSupport.getFinalPath(WindowsLinkSupport.java:107)
    at sun.nio.fs.WindowsFileStore.create(WindowsFileStore.java:83)
    at sun.nio.fs.WindowsFileSystemProvider.getFileStore(WindowsFileSystemProvider.java:482)
    at java.nio.file.Files.getFileStore(Files.java:1461)
    at com.jthink.songkong.Fs.main(Fs.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • If the drive is on a remote windows, that limit is still to check, no ? So if you found that this is a windows drive, that's enough. – AxelH Sep 06 '17 at 12:38
  • @AxelH No I dont think so, might be using Windows simply to access a NAS that uses NTFS for example, and the NAS doesnt need to stick to this limit, as it is a Windows UI limit rather than an NTFS limit. – Paul Taylor Sep 06 '17 at 12:41
  • 1
    You might find your answer here: [Java: how to determine the type of drive a file is located on?](https://stackoverflow.com/questions/9163707/java-how-to-determine-the-type-of-drive-a-file-is-located-on) – CraigR8806 Sep 06 '17 at 12:42
  • NTFS? Did you mean NFS? I wouldn't use NTFS for a NAS, that's all – OneCricketeer Sep 06 '17 at 12:44
  • @cricket_007 I heard that Windows is going to be migrating to a new format NaFS (Not a File System) – CraigR8806 Sep 06 '17 at 12:45
  • @cricket_007 nfs is a way of remotely mounting a filestore, it is not the filestore itself, my Netgear ReadyNas comes formatted as NTFS. – Paul Taylor Sep 06 '17 at 13:50

0 Answers0