10

如何使用 Java 在 Windows 中获取长文件名的短文件名?

我需要使用 Java(tm) 确定存储在 Windows 系统上的文件的短文件名。

4

2 回答 2

10

自我回答

有相关的问题和相关的答案。然而,我发布了这个解决方案,因为它使用 Java(tm) 代码而不需要外部库。欢迎使用针对不同版本的 Java 和/或 Microsoft(R) Windows(tm) 的其他解决方案。

主要概念

主要概念在于通过运行时类从 Java(tm) 调用 CMD:

cmd /c for %I in ("[long file name]") do @echo %~fsI

解决方案

在 Windows 7 系统上运行的 Java SE 7 上进行了测试(为简洁起见,代码已减少)。

    public static String getMSDOSName(String fileName)
    throws IOException, InterruptedException {

    String path = getAbsolutePath(fileName);

    // changed "+ fileName.toUpperCase() +" to "path"
    Process process =
        Runtime.getRuntime().exec(
            "cmd /c for %I in (\"" + path + "\") do @echo %~fsI");

    process.waitFor();

    byte[] data = new byte[65536];
    int size = process.getInputStream().read(data);

    if (size <= 0)
        return null;

    return new String(data, 0, size).replaceAll("\\r\\n", "");
}

public static String getAbsolutePath(String fileName)
    throws IOException {
    File file = new File(fileName);
    String path = file.getAbsolutePath();

    if (file.exists() == false)
        file = new File(path);

    path = file.getCanonicalPath();

    if (file.isDirectory() && (path.endsWith(File.separator) == false))
        path += File.separator;

    return path;
}
于 2013-09-19T11:23:17.287 回答
-1

我发现了一个小问题 Osmund 的解决方案。由于某种原因,此文件名无法正常工作:

N:\directoryY\tmp\temp\asdfasdf sdf dsfasdf [dfadss]\[asdfas] asdfasd asd asdfasdf ~fdfsdfdfdsfdfdfdfdfd~ TTTm7-9  [RR 1234a5678 A.888 OKOK]a

我不太确定为什么。但是,如果您以稍微不同的方式运行该命令(使用 ProcessBuilder),它就可以工作。这是新代码(我正在使用 BufferedReader 读取输出,它更干净)。

    public static String getMSDOSName(String path) throws IOException, InterruptedException {
        Process process = new ProcessBuilder().command("cmd", "/c", "for %I in (\"" + path + "\") do @echo %~fsI").start();
        process.waitFor();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            return br.readLine();
        }
    }

这是原始解决方案与我的解决方案的输出。原始解决方案未能缩短最后一个路径元素:

N:\DIRECT~1\tmp\temp\ASDFAS~1\[asdfas] asdfasd asd asdfasdf ~fdfsdfdfdsfdfdfdfdfd~ TTTm7-9 [RR 1234a5678 A.888 OKOK]a
N:\DIRECT~1\tmp\temp\ASDFAS~1\_ASDFA~1.888
于 2020-07-11T21:46:32.923 回答