仅限Windows计算机需要此选项来决定是否强制执行Windows资源管理器259路径字符串限制。我有以下方法使用 Filestore 确定分区是否是基于Windows的分区,但我还需要知道本地或远程驱动器。
我正在使用Java 8.
更新
我尝试对Z:\上安装的网络驱动器的根目录,这实际上抛出一个异常,这有助于确定是否联网,但我不知道如何知道Z:\ Drive正在挂载\ nas因为如果我想要找到我需要使用的文件\ 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;
}
}
我更喜欢一个合适的纯Java实现,作为可能的重复Java: how to determine the type of drive a file is located on?给出的答案是相当片段的代码
更新
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);
}
}
输出
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)