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

时间:2013-09-19 11:23:17

标签: java windows short-filenames

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

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

2 个答案:

答案 0 :(得分:9)

自我答案

有相关问题及相关答案。但是,我发布了这个解决方案,因为它使用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;
}

答案 1 :(得分:-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