gpt4 book ai didi

Java用另一个程序打开文件

转载 作者:行者123 更新时间:2023-11-29 07:54:28 28 4
gpt4 key购买 nike

我想在我的 java 程序中打开一个文件,但我无法让它工作。我希望我的程序可以在所有平台上运行,尤其是在 ubuntu 上。

这是我的:

private void openFileChoser() {
JFileChooser chooser = new JFileChooser(".");
int returnVal = chooser.showOpenDialog(getParent());
if(returnVal == JFileChooser.APPROVE_OPTION) {
openFile(chooser.getSelectedFile().getAbsolutePath());
} else {
System.out.println("Aucun fichier choisi");
}
}

/***
* ouverture du fichier :
* @param name
*/
private void openFile(String name) {
System.out.println("Ouverture du fichier: " +
name);
try {
Runtime.getRuntime().exec(new String[]{name});
//Desktop.getDesktop().open(new File(name)); //doesn't work on ubuntu 13.04

} catch (Exception e) {
System.err.println("Erreur d'ouverture du fichier : "+name);
e.printStackTrace();
}
}

我试图打开文件:/home/user/IdeaProjects/androidresourcehelper/test.xls

我得到了异常(exception):

java.io.IOException: Cannot run program "/home/user/IdeaProjects/androidresourcehelper/test.xls": error=13, Permission denied
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at java.lang.Runtime.exec(Runtime.java:617)
at java.lang.Runtime.exec(Runtime.java:485)
at com.vaxapp.android.ressources.parser.AppContentPane.openFile(AppContentPane.java:118)
at com.vaxapp.android.ressources.parser.AppContentPane.openFileChoser(AppContentPane.java:104)
at com.vaxapp.android.ressources.parser.AppContentPane.actionPerformed(AppContentPane.java:96)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.io.IOException: error=13, Permission denied
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
... 42 more

我想知道如何指定一个程序来打开 EXCEL 文件。理想的情况是获取系统上可用的程序列表以打开此类文件,例如,如果 Windows excel 可以用它打开,但如果我们在 ubuntu 上,例如使用 open office,而在 Mac 上使用其他东西。我在哪里可以找到有关如何执行此操作的信息?

我设法打开了一个 XML 文件:

Runtime.getRuntime().exec(new String[]{"gedit","strings.xml});

但这只有在安装了 gedit 的情况下才有效,我不确定它是否适用于 Windows。现在我只需要打开 xls 和 xml 文件。提前致谢

编辑:

感谢 Peter 的提示和此链接: How do I programmatically determine operating system in Java?

我已经设法创建了这个临时解决方案:

 private void openFile(String path) {
System.out.println("Ouverture du fichier: " +
path);
try {
Tools.OsType osType = Tools.getOsType();
if (osType == Tools.OsType.LINUX) {
Tools.FileType fileType = Tools.getFileType(path);
if(fileType == Tools.FileType.XML) {
Runtime.getRuntime().exec(new String[]{"gedit",path});
} else if(fileType==Tools.FileType.CALC) {
openXlFile(path);
} else {
System.err.println("Extension inconnue");
}
} else {
Desktop.getDesktop().open(new File(path));
}
} catch (Exception e) {
System.err.println("Erreur d'ouverture du fichier : "+path);
e.printStackTrace();
}
}

//localc == libre office
//oocalc == open office
private void openXlFile(String path) throws IOException {
try {
Runtime.getRuntime().exec(new String[]{"localc",path});
} catch (Exception e) {
Runtime.getRuntime().exec(new String[]{"oocalc",path});
}
}

最佳答案

Desktop.open() 只有在系统知道默认为该文件类型打开哪个应用程序时才有效。如果设置不正确,这不是您可以从 Java 修复的问题。您可以通过猜测应该使用哪个程序来解决这个问题,正如您所做的那样,但真正的解决方法是正确设置您的操作系统。

关于Java用另一个程序打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18804784/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com