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)