在Java中创建文件时,如何确定允许的最大路径长度

在Java中创建文件时,如何确定允许的最大路径长度,java,file-io,nio,Java,File Io,Nio,在Java中创建文件时,如何确定允许的最大路径长度。 我使用的是Java 7,因此如果有帮助的话,我可以使用Java NIO2,但我如何确定文件系统上允许的文件最大长度,并确保我不尝试创建具有如此无效路径的文件 我希望在尝试创建之前缩短路径(子字符串),而不是在创建之后处理问题,特别是因为异常/错误消息可能因文件系统而异 我不想硬性规定每个平台的代码长度,因为Windows应用程序可以访问OSX文件系统ectera,所以这根本不起作用 它仅在尝试创建文件时失败 i、 e 给予 java.nio.

在Java中创建文件时,如何确定允许的最大路径长度。 我使用的是Java 7,因此如果有帮助的话,我可以使用Java NIO2,但我如何确定文件系统上允许的文件最大长度,并确保我不尝试创建具有如此无效路径的文件

我希望在尝试创建之前缩短路径(子字符串),而不是在创建之后处理问题,特别是因为异常/错误消息可能因文件系统而异

我不想硬性规定每个平台的代码长度,因为Windows应用程序可以访问OSX文件系统ectera,所以这根本不起作用

它仅在尝试创建文件时失败 i、 e

给予

java.nio.file.FileSystemException: C:\User\Mesh\kkkkkkkkkkkkkkkkkkkkkkkkkkrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk.txt: The filename, directory name, or volume label syntax is incorrect.

    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:229)
    at java.nio.file.Files.newByteChannel(Files.java:315)
    at java.nio.file.Files.createFile(Files.java:586)
与Java6方法比较

try
        {
            //File file = new File("C:/User/Mesh/kkkkkkkkkkkkkkkkkkkkkkkkkkrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk.txt");
            Path p = Paths.get("C:/User/Mesh/kkkkkkkkkkkkkkkkkkkkkkkkkkrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk.txt");
            p.toFile().createNewFile();
            //boolean result=file.isFile();
            //System.out.println("Result is:"+result);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
给出了完全不同的错误

java.io.IOException: The filename, directory name, or volume label syntax is incorrect
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:947)
但实际上是不相关的,因为我想在创建文件之前做一些事情

编辑:
看起来255个字符是一个很好的默认值

尝试以下内容:

boolean success = true;
File testFile = new File("/a/really/long/name");
try {
    success = testFile.createNewFile();
} catch (IOException ioe) {
    // anyway check the exception, the problem could have been other
    // for example: duplicate name, lack of permissions, etc.
    success = false;
}

if (!success) {
    // error creating file
}

java.nio.file.FileSystem
中有一个方法
getPath
,可用于创建有效的路径名,否则会引发InvalidPathException。也许这会对您有所帮助。

这对它在任何系统上工作都有什么帮助呢?不仅仅是我自己的系统。要记住的一点是,如果您使用Eclipse,它有自己的(相当严重的)路径长度限制。我无法立即找到实现此值的方法。可能最好是在试图打开文件并在那里处理时捕获异常。我想要的是一种在创建文件之前检查文件名是否有效的方法。@PaulTaylor我认为这是不可能的。最简单的方法是尝试创建它,并在出现错误的情况下处理它,如上所示,这看起来很有用,但我认为现在我只需要将其编码为255个字符
boolean success = true;
File testFile = new File("/a/really/long/name");
try {
    success = testFile.createNewFile();
} catch (IOException ioe) {
    // anyway check the exception, the problem could have been other
    // for example: duplicate name, lack of permissions, etc.
    success = false;
}

if (!success) {
    // error creating file
}