gpt4 book ai didi

java - 打开文件名中包含外来字符的文件

转载 作者:太空宇宙 更新时间:2023-11-04 07:36:40 24 4
gpt4 key购买 nike

我正在为下个月的考试开发一个小型应用程序。在此应用程序中,用户可以上传与其名称匹配的文件。

不幸的是,在丹麦,我们有一些外来字符 (ÆØÅæøå),我的代码将无法打开包含这些字符的文件。

是否有解决方法可以让我使用这些文件名?

这是打开文件的代码:

Desktop.getDesktop().open(new File("kontrakt/" + getStuderende().getKontrakt()));

异常(exception)情况:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: kontrakt/5_Bjørnen-Bjørn-kontrakt.docx doesn't exist.
at java.awt.Desktop.checkFileValidation(Desktop.java:209)
at java.awt.Desktop.open(Desktop.java:270)
at View.StuderendePanel.visKontraktBtnActionPerformed(StuderendePanel.java:382)
at View.StuderendePanel.access$1000(StuderendePanel.java:29)
at View.StuderendePanel$12.actionPerformed(StuderendePanel.java:284)
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.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:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
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:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
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)

最佳答案

该代码可以在 Windows 中运行。但是,本主题中使用的 OSX 操作系统显然不支持此功能。

这很好用

package others;

import java.awt.Desktop;
import java.io.File;

public class A {

public static void main(String[] args) throws Exception{


Desktop.getDesktop().open(new File("c://5_Bjørnen-Bjørn-kontrakt.docx"));

}

}

尝试打印您从 getStuderende().getKontrakt() 方法获得的内容

检查它是否与您正在打开的文件相同。

关于java - 打开文件名中包含外来字符的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16769986/

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