gpt4 book ai didi

java - 为什么 JFileChooser 会这样工作?

转载 作者:行者123 更新时间:2023-12-02 03:31:08 26 4
gpt4 key购买 nike

大家好,好久不见了。所以我正在开发这个应用程序,它允许我制作和保存文档等等。这不是这个问题的重点。因此,在保存功能中,我决定使用 JFileChooser 来允许用户选择保存文件的位置。

现在我的 JFileChooser 可以正常启动,下面是代码:

 public void Save () {
choicer.setCurrentDirectory(new java.io.File("C://"));
choicer.setDialogTitle("Save Document");
choicer.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
choicer.setAcceptAllFileFilterUsed(false);
if (choicer.showSaveDialog(new JPanel()) == JFileChooser.APPROVE_OPTION) {
dir = String.valueOf(choicer.getCurrentDirectory());
}

File f = new File(dir + "\\hi.txt");
}

忽略 hi.txt 名称。我稍后会重点讨论。

现在我发现了两个问题。首先,我必须“深入”一个文件夹来保存我的文件。 说明:
假设我想将我的文件保存在用户的公共(public)目录中,我的目录如下所示:

C:\Users\Public
现在我的代码在文件参数中添加了 \hi.txt ,这不应该保存在公共(public)文件夹中吗?如果我在公开后只是保存在那里,我会得到一个异常(exception):

java.io.FileNotFoundException: C:\Users\Public (Access is denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at Spreadr$TableMethods.Save(Spreadr.java:175)
at Spreadr.lambda$1(Spreadr.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

如果我公开打开一个文件夹,然后单击“保存”,我的目录现在看起来像:

C:\Users\Public\Downloads

然后,它不会保存在“下载”文件夹中,而是保存在“公共(public)用户”文件夹中。

这是我的第一个问题。我的第二个实际上是一个迷你问题类型的东西。我可以在“保存”菜单(JFileChooser 菜单)中命名我的文件吗?

最佳答案

您的代码调用 choicer.getCurrentDirectory() ,这将为您提供在 JFileChooser.DIRECTORIES_ONLY 模式下选择目录时所在目录的父目录。

您可能想要获取您选择的目录,您可以通过调用

choicer.getSelectedFile()

关于java - 为什么 JFileChooser 会这样工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38069384/

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