Java如何规避Windows MAX_PATH WinAPI限制 [英] How does Java circumvent the windows MAX_PATH WinAPI limitation

查看:348
本文介绍了Java如何规避Windows MAX_PATH WinAPI限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道Java如何能够规避Windows MAX_PATH限制。使用下面的代码,我能够在Java中创建一个非常长的路径,并且能够执行I / O,如果不使用前缀为\\?\而无法使用Windows。

Does anyone know how Java is able to circumvent the windows MAX_PATH limitations. Using the below code I was able to create a really long path in Java and was able to perform I/O, which would have been impossible using windows without prefixing \\?\.

public static void main(String[] args) throws IOException {
    BufferedWriter bufWriter = null;
    try {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < 130; i++) {
            s.append("asdf\\");
        }
        String filePath = "C:\\" + s.toString();;
        System.out.println("File Path = " + filePath);
        File f = new File(filePath);
        f.mkdirs();
        f = new File(f, "dummy.txt");
        System.out.println("Full path = " + f);
        bufWriter = new BufferedWriter(new FileWriter(f));
        bufWriter.write("Hello"); 

    } 
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (bufWriter != null) {
            bufWriter.close();
        }
    }
}


推荐答案

来自JVM的 canonicalize_md.c

/* copy \\?\ or \\?\UNC\ to the front of path*/
WCHAR* getPrefixed(const WCHAR* path, int pathlen) {
    [download JVM source code (below) to see implementation]
}

函数 getPrefixed :


  • 函数 wcanonicalize 如果((pathlen = wcslen(路径))> MAX_PATH - 1)

  • 由函数 wcanonicalizeWithPrefix

  • by the function wcanonicalize if ((pathlen = wcslen(path)) > MAX_PATH - 1)
  • by the function wcanonicalizeWithPrefix.

我没有追踪到比这更远的调用链,但我认为JVM始终是在访问文件系统之前使用这些规范化例程,因此始终以这种或那种方式命中此代码。如果你想自己追踪更远的调用链,你也可以参与浏览JVM源代码的乐趣!下载地址: http://download.java.net/openjdk/jdk6/

I didn't trace the call chain farther than that, but I assume the JVM always uses these canonicalization routines before accessing the filesystem, and so always hits this code one way or another. If you want to trace the call chain farther yourself, you too can partake in the joys of browsing the JVM source code! Download at: http://download.java.net/openjdk/jdk6/

这篇关于Java如何规避Windows MAX_PATH WinAPI限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆