有没有办法在 Java 中生成 8.3 或“短"(Windows)版本的文件名? [英] Is there a way to generate the 8.3 or 'short' (Windows) version of a file name in Java?

查看:38
本文介绍了有没有办法在 Java 中生成 8.3 或“短"(Windows)版本的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序中,我们允许用户打开文件和目录.

In our application, we are allowing users to open files and directories.

Java 6 为我们提供了...

Java 6 provides us with...

java.awt.Desktop.getDesktop().open(file);

效果很好.但是,由于我们需要保证Java 5的兼容性,所以我们也实现了一种通过调用cmd.exe...

which works great. However, since we need to ensure Java 5 compatibility, we also implement a method of opening files by calling the start command in cmd.exe...

String command = "cmd.exe start ...";
Runtime.getRuntime().exec(command);

这就是问题出现的地方.看来 start 命令只能处理 8.3 文件名,这意味着任何非短 (8.3) 文件/目录名称都会导致 start 命令失败.

This is where the problem shows up. It seems that the start command can only handle 8.3 file names, which means that any non-short (8.3) file/directory names cause the start command to fail.

有没有一种简单的方法来生成这些短名称?或其他任何解决方法?

Is there an easy way to generate these short names? Or any other workarounds?

推荐答案

尝试这样的事情

import java.io.IOException;

class StartExcel {
    public static void main(String args[])
        throws IOException
    {
        String fileName = "c:\\temp\\xls\\test2.xls";
        String[] commands = {"cmd", "/c", "start", "\"DummyTitle\"",fileName};
        Runtime.getRuntime().exec(commands);
    }
}

将虚拟标题传递给 Windows 启动命令很重要,因为文件名可能包含空格.这是一个特点.

It's important to pass a dummy title to the Windows start command where there is a possibility that the filename contains a space. It's a feature.

这篇关于有没有办法在 Java 中生成 8.3 或“短"(Windows)版本的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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