在默认文本编辑器中打开一个文本文件...通过 Java?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6273221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open a text file in the default text editor... via Java?
提问by Nathan Hughes
OK. Simple question. Maybe not so simple answer, though:
行。简单的问题。不过,也许不是那么简单的答案:
I have a file I downloaded in Java, and I know that it's a text file. Is there any way that I can use Java to open that text file in whatever the default text editor is? It has to work for all OS's, otherwise I would just make it open with Notepad.
我有一个用 Java 下载的文件,我知道它是一个文本文件。有什么方法可以使用 Java 在默认文本编辑器中打开该文本文件?它必须适用于所有操作系统,否则我只会用记事本打开它。
:\ I guess that if there's no way to do this I could use JOptionPane and show the contents of the text file...
:\ 我想如果没有办法做到这一点,我可以使用 JOptionPane 并显示文本文件的内容......
回答by Nathan Hughes
You can do that with:
你可以这样做:
java.awt.Desktop.getDesktop().edit(file);
This links to the tutorial article on java.awt.Desktop:
这链接到关于 java.awt.Desktop 的教程文章:
Java? Standard Edition version 6 narrows the gap between performance and integration of native applications and Java applications. Along with the new system tray functionality, splash screen support, and enhanced printing for JTables , Java SE version 6 provides the Desktop API (java.awt.Desktop) API, which allows Java applications to interact with default applications associated with specific file types on the host platform.
爪哇?标准版第 6 版缩小了本机应用程序和 Java 应用程序的性能和集成之间的差距。除了新的系统托盘功能、启动画面支持和增强的 JTables 打印外,Java SE 版本 6 还提供了桌面 API (java.awt.Desktop) API,它允许 Java 应用程序与与特定文件类型相关联的默认应用程序进行交互主机平台。
It is cross-platform, but may not be supported everywhere. There is a method you can call to check whether the Desktop API is available, called isDesktopSupported (see the link for more explanation). I was using this API the other day to open PDFs in a Swing client.
它是跨平台的,但可能并非所有地方都支持。您可以调用一个方法来检查桌面 API 是否可用,称为 isDesktopSupported(更多解释请参见链接)。前几天我正在使用这个 API 在 Swing 客户端中打开 PDF。
Unfortunately there is a known bug affecting some Windows platforms (XP and 2003)that will crash the JVM. Write once, debug everywhere, as usual. Anyway, for Windows there is a nice workaroundwhich still uses the user's preferred application:
不幸的是,有一个已知的错误会影响某些 Windows 平台(XP 和 2003),这会导致 JVM 崩溃。像往常一样编写一次,到处调试。无论如何,对于 Windows,有一个很好的解决方法,它仍然使用用户的首选应用程序:
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
String cmd = "rundll32 url.dll,FileProtocolHandler " + file.getCanonicalPath();
Runtime.getRuntime().exec(cmd);
}
else {
Desktop.getDesktop().edit(file);
}
回答by Stas Jaro
Desktop.getDesktop().edit(File f);
回答by Hot Licks
Certainly you could configure in the text editor and use Runtime.exec to start it. But I can't think of any way to determine the default editor, especially in a system-independent fashion.
当然,您可以在文本编辑器中进行配置并使用 Runtime.exec 来启动它。但是我想不出任何方法来确定默认编辑器,尤其是在独立于系统的方式中。
Maybe your best option is to identify which of the several most popular platforms you're on and then find a way to start the default editor on that platform. Eg, on Window you'll get the default editor if you do "start filename.txt", and I'm pretty sure there's a Linux equivalent.
也许您最好的选择是确定您在几个最流行的平台中的哪一个,然后找到一种方法在该平台上启动默认编辑器。例如,在 Window 上,如果您执行“start filename.txt”,您将获得默认编辑器,而且我很确定有一个 Linux 等价物。